ASP.NET Web API error “Uncaught SyntaxError: Unexpected token :”
I am very new to asp.net web api. I am making a simple call with jquery, cross-domain. Everything seems to work properly and when I even check the results I get back, I see that it sends json back to me, but the call fails with the “Uncaught SyntaxError: Unexpected token :” error. I am not sure why its failing, Thanks for any help.
$.ajax({
url: 'http://webapidomain.domain.com/api/Register?firstName=' + firstName + '&lastName=' + lastName + '&' + 'email=' + email + '&password=' + password,
type: 'GET',
dataType: 'jsonp',
contentType: 'application/json',
success: function (result) {
alert(result);
},
complete: function () {
$.mobile.hidePageLoadingMsg();
}
});
Web API does not support JSONP out of the box. You would need a JSONP media-type formatter. (The media-type formatter is the object that serializes the data to a particular format, such as JSON or XML.)
Rick Strahl has some code here that might help: http://www.west-wind.com/weblog/posts/2012/Apr/02/Creating-a-JSONP-Formatter-for-ASPNET-Web-API (I haven't tried it.)
The reason you are getting back JSON when you asked for JSONP is that when Web API cannot match the type that you requested, it returns the first format in its list, which by default is JSON.
if you are making a cross domain call it is by default blocked by many browsers by the SAME-ORGIN Policy. If you need to work around that you need to use JSONP (if you can modify the output produced by the end point that you are calling ie you own the domain) else you need to use your web server as a proxy and connect to that end point and query to your web endpoint to get the corresponding data.
Also there are certain websites that will do this (act as the proxy to generate JSONP result) for you and produce a jsonp result.
链接地址: http://www.djcxy.com/p/45966.html上一篇: 使用Ajax在PHP中发表评论