Why can't I access my WCF web service with Javascript?

后端 未结 2 810
眼角桃花
眼角桃花 2020-12-22 04:57

I\'m new at AJAX and I\'m trying to access a WCF web service the following way:

$(function () {
    $(\'#formNew\').submit(function () {
        var datos =          


        
2条回答
  •  星月不相逢
    2020-12-22 05:29

    I normally use the following configuration to enable ajax calls to my WCF services:

    1) First I create a JSON endpoint behaviour in Web.config and associate my service to it:

    
      
        
          
            
          
        
      
      
        
          
        
      
    
    

    2) Then I can simply define my WCF service like this:

    [ServiceContract]
    public class LoginService
    {
        [OperationContract]
        public void SignIn(string email, string pswd)
        {
            // Check credentials and create session cookie
        }
    }
    

    3) And finally make jQuery ajax calls like showed below:

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        url: serviceUrl + '/SignIn',
        type: 'POST',
        data: JSON.stringify({
            email: 'john.doe@abc.com', 
            pswd: 'qwerty'
        }),
        success: function () { alert('success!'); },
        error: function () { alert('error!'); }
    });
    

提交回复
热议问题