C# 使用JSONP调用restapi&;JQuery
我已经在MVC4中创建了RESTAPI,当我编写来自fiddler的请求时,它工作得很好。但在我的应用程序中,我需要通过jsonp调用,因为它会跨域请求。但是,当我调用此服务时,会出现如下错误: Jquery JsonP调用C# 使用JSONP调用restapi&;JQuery,c#,asp.net-mvc,jsonp,C#,Asp.net Mvc,Jsonp,我已经在MVC4中创建了RESTAPI,当我编写来自fiddler的请求时,它工作得很好。但在我的应用程序中,我需要通过jsonp调用,因为它会跨域请求。但是,当我调用此服务时,会出现如下错误: Jquery JsonP调用 $.ajax({ type: "POST" , url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf&callback=?",
$.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);
},
});
错误:
现在您没有在做JSONP。它仍然是POST请求。要使其成为JSONP,只需向
$.ajax()
调用添加数据类型:“JSONP”
。您还可以删除一些其他冗余参数,如内容类型和“回调”参数(但这是可选的)。因此,您的代码应该如下所示:
$.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) { /* ... */ },
});
还要准备好,您的请求将被转换为GET-one,并且看起来像
/GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain=ppp&resllerId=123&&u972349857
因此,请为此准备服务器端代码。也许这有助于: