Hodaya Shalom
Hodaya Shalom

Reputation: 4417

XmlHttpRequest status 0 even when the call to service succeeded

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?

Upvotes: 0

Views: 889

Answers (2)

Hodaya Shalom
Hodaya Shalom

Reputation: 4417

My problem was that in User object I had DateTime.

When I turned it to String it worked.

Upvotes: 0

Khanh TO
Khanh TO

Reputation: 48972

It could be that you're making a cross-domain request. Note that the same host name with different ports are considered different domain. For example: http://localhost:20 and http://localhost:40 are considered different domains.

In your case, it could be that your browser supports CORS and therefore requests to a different domain are still sent. That's why when you debug on server side you see it works, but when the browser receives response from the server, the response is discarded and an error is raised because of missing Access-Control-Allow-Origin header from the response or having the Access-Control-Allow-Origin header but with a value different than your domain.

Upvotes: 1

Related Questions