0

I call to Wcf Service via Jquery(JS), after a long time of trying it works.

The call to service is as follows:

function CallService() {   
    var request = { userName: "aaa", password: "123" };
    var jsondata = JSON.stringify(request);   
    $.ajax({
        type: "POST", //GET or POST or PUT or DELETE verb
        url: "http://localhost:xxxx/Service1.svc/GetUser", // Location of the service
        data: jsondata, //Data sent to server
        contentType: "application/json; charset=utf-8", // content type sent to server
        dataType: "json", //Expected data format from server
        processdata: true, //True or False
        crossDomain: true, //True or False
        success: function (result) {
            alert('success');
        },
        complete: function () {
            alert('completed');
        },
        error: function (xhr, status, message) {
            alert('error with status - ' + xhr.status);
        }
    });  
}

I put BreakPoint in GetUser function that is in service and when I call the function CallService I go to the BreakPoint of Service (which means it works!).

Service function works great and returns the correct data, but to Jquery I get the Error function with status 0.

In addition, in the console I see a red error:(Not generally understood as an error)

POST http://localhost:xxx/Service1.svc/GetUser  

What could be the problem?

4

2 回答 2

1

可能是您正在发出跨域请求。请注意,具有不同端口的相同主机名被视为不同的域。例如:http://localhost:20http://localhost:40被认为是不同的域。

在您的情况下,可能是您的浏览器支持 CORS,因此仍会发送对不同域的请求。这就是为什么当您在服务器端调试时看到它可以工作,但是当浏览器从服务器接收到响应时,响应被丢弃并引发错误,因为Access-Control-Allow-Origin响应中缺少标头或具有Access-Control-Allow-Origin标头但值与您的不同领域。

于 2013-07-04T12:48:55.107 回答
0

我的问题是User我拥有的对象DateTime

我把它转向它String时。

于 2013-07-07T10:02:37.727 回答