I have created REST API in MVC4, it is working fine when I compose request from fiddler. But in my application, I need to call through jsonp because it would cross domain request. But when I'm calling this service it gives me error as shown below:
我已经在MVC4中创建了REST API,当我从fiddler中编写请求时,它可以正常工作。但是在我的应用程序中,我需要通过jsonp调用,因为它会跨域请求。但是当我调用这个服务时它会给我错误,如下所示:
Jquery JsonP Call ..
Jquery JsonP叫. .
$.ajax({
type: "POST" ,
url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf&callback=?",
data: { SubDomain: subDomain, ParentDomain: parentDomain, ResellerId: resellerId },
cache: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
if (callback)
callback(response.d);
},
error: function (response) {
if (callback)
error(response.d);
},
});
Error:
错误:
1 个解决方案
#1
2
Right now you are not doing JSONP. It's still POST request. To make it JSONP you need simply to add dataType: "jsonp"
to you $.ajax()
call. You can also remove some other redundancy parameters like content-type and 'callback' param (but that's optional). So, your code should looke like:
现在你不是在做JSONP。它仍然是POST请求。要使它成为JSONP,只需将dataType:“JSONP”添加到$.ajax()调用。您还可以删除一些其他冗余参数,如内容类型和“回调”参数(但这是可选的)。因此,您的代码应该如下所示:
$.ajax({
url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf",
data: { SubDomain: subDomain, ParentDomain: parentDomain, ResellerId: resellerId },
datatype: "jsonp",
cache: false,
success: function (response) { /* ... */ },
error: function (response) { /* ... */ },
});
Be also ready, that your request will be transformed to a GET one and will look like /GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain=ppp&ResellerId=123&_=4398572349857
还要做好准备,您的请求将被转换为GET,并且看起来像/GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain= ppresellerid = 123_4398572349857
So, prepare your server-side code for that.
因此,准备好您的服务器端代码。
#1
2
Right now you are not doing JSONP. It's still POST request. To make it JSONP you need simply to add dataType: "jsonp"
to you $.ajax()
call. You can also remove some other redundancy parameters like content-type and 'callback' param (but that's optional). So, your code should looke like:
现在你不是在做JSONP。它仍然是POST请求。要使它成为JSONP,只需将dataType:“JSONP”添加到$.ajax()调用。您还可以删除一些其他冗余参数,如内容类型和“回调”参数(但这是可选的)。因此,您的代码应该如下所示:
$.ajax({
url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf",
data: { SubDomain: subDomain, ParentDomain: parentDomain, ResellerId: resellerId },
datatype: "jsonp",
cache: false,
success: function (response) { /* ... */ },
error: function (response) { /* ... */ },
});
Be also ready, that your request will be transformed to a GET one and will look like /GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain=ppp&ResellerId=123&_=4398572349857
还要做好准备,您的请求将被转换为GET,并且看起来像/GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain= ppresellerid = 123_4398572349857
So, prepare your server-side code for that.
因此,准备好您的服务器端代码。