2012-11-13 33 views
2

我在MVC4中创建了REST API,当我从提琴手撰写请求时它工作正常。但在我的应用程序中,我需要通过jsonp调用,因为它会跨域请求。但是,当我调用这个服务它给我的错误,如下图所示:使用JSONP和JQuery调用REST API

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); 
     }, 
    }); 

错误: enter image description here

+0

也许这可能有所帮助:http://stackoverflow.com/questions/5241088/jquery-call-to-webservice-returns-no-transport-error – mipe34

+0

它不适用于JSONP –

回答

2

现在你是不是做JSONP。它仍然是POST请求。为了使它成为JSONP,你只需要简单地将dataType: "jsonp"添加到你的$.ajax()调用中即可。你也可以删除一些其他的冗余参数,比如content-type和'callback'参数(但这是可选的)。所以,你的代码应该looke像:

$.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) { /* ... */ }, 
}); 

也做好准备,你的请求将被转化为找一本看起来像 /GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain=ppp&ResellerId=123&_=4398572349857

因此,对于准备你的服务器端代码。