Reputation: 1478
I have an issue calling web service which is in cross-domain. I've read some articles here about it, but I didn't really find a solution. I've just understood that I need the json
format of the data, because I was always getting Error: Access denied.
while trying to get xml
data from service, but now I have a different problem. Here is my .ajax()
call:
$.ajax({
type: "GET",
contentType: "application/jsonp; charset=utf-8",
url: "http://tomas/_vti_bin/EmmaService.asmx/GetResult",
dataType: "jsonp",
data: {
value : "testValue",
converstionId : "testId"
},
success: function(resp) {
alert("success: " + resp);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("error status: " + xhr.status);
alert("error status text: " + xhr.statusText);
alert("error response text: " + xhr.responseText);
},
});
From this I get error with 3 following alerts:
error status: 200
error status text: success
error response text: undefined
What I don't understand is error status text: success
.
Code in my web service:
[WebMethod(EnableSession = false, Description = "Gets result")]
public EmmaServiceResult GetResult(string value, string converstionId)
{
...
return result;
}
Any suggestions on how to get this working? Thanks! :)
Upvotes: 1
Views: 3677
Reputation: 591
I recently had a lot of issues at work making a cross-domain request from an AJAX call. We ended up getting it working without having to modify the API, but we did need access to the server hosting the API so we could have it send down some headers in the response. But the whole issue was a pain to debug, and I found that all browsers were terrible about reporting meaningful errors. So potentially this might not work for you and apologies in advance if this doesn't address your issue.
The solution requires you make a CORS request, and add some headers to your server response. These pages were both good resources:
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS http://www.html5rocks.com/en/tutorials/cors/
I think in your case, since you're making a basic request and you're not dealing with cookies, you can leave your .ajax call essentially unchanged, just changing dataType to "json" and contentType to "application/json" if you're sending JSON.
You'll then have to modify the server to have it handle the CORS preflight request by adding these headers to the response:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: Content-Type
(See this question: jQuery CORS Content-type OPTIONS)
Hopefully this will work for you!
Upvotes: 0
Reputation: 1149
Try adding ?callback=?
to the end of your URL:
http://tomas/_vti_bin/EmmaService.asmx/GetResult?callback=?
Also, try looking at the thrownError to determine what the error is:
alert("error response text: " + thrownError);
It could be a parsing error, etc.. something not actually related to the ajax request, but how you define how the response should be handled.
Also, look here to see how to return json from a WCF service.
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "players")]
Upvotes: 2