jQuery jsonp ajax call doesn't return results when called over ssl
I have the following ajax call, called from a ssl page in www.DomainName.com (e.g. https://www.domainname.com/caller.aspx):
$.ajax({
type: "GET",
url: "https://services.DomainName.com/myService.svc/GetCustomers",
data: { accNo: accno, count: "35" },
dataType: "jsonp",
error: function (xhr, status, error) {
var test = "";
var test2 = xhr;
var test3 = stat;
var test4 = error;
},
success: function (data) {
var suggestions = [];
$.each(data, function (i, val) {
suggestions.push(v开发者_StackOverflowal);
});
add(suggestions);
}
})
Neither the success nor the error functions are called.
But when I change the service url to not use ssl the call succeeds and returns a list of customers: http://services.DomainName.com/myService.svc/GetCustomers
If I paste https://services.DomainName.com/myService.svc/GetCustomers directly in the browser's address bar, a list of customers is returned. It's the jasonp call that doesnt return a result (nor error) when called over SSL.
Any idea?
Just so anyone that comes across this in the future doesn't struggle with this as I did, the answer to this is to add the security tag in your config file as follows:
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" >
<security mode ="Transport">
<transport clientCredentialType= "None" />
</security>
</binding>
</webHttpBinding>
</bindings>
When I had this problem, I could see in fiddler that calling the rest call with http returned the results with a callback function in the results, but calling the same rest call with https only returned the results, which were not wrapped in the callback function.
Here is an example of what i was setting in fiddler and is as symptom that you have this problem.
HTTPS REST CALL:
https://mydomain.com/service.svc/GetState?callback=jQuery171014469725495108632_1368658094950&_=1368658098533
RETURNED: "{\"statename\":null,\"stateid\":null,\"region\":null}"
HTTP REST CALL: http://mydomain.com/service.svc/GetState?callback=jQuery171013910021002618056_1368658212752&_=1368658215502
RETURNED: jQuery171013910021002618056_1368658212752("{\"statename\":null,\"stateid\":null,\"region\":null);
You can see that the https call didn't correctly wrap the result with the jquery callback function. Using the security tag fixed it. Hope this helps the next guy. Robert
精彩评论