2016-08-03 91 views
0

我尝试使用jQuery AJAX使用REST API发布数据。我的代码如下,回调HTTP 400错误

$.ajax({ 
    url: 'myurl', 
    type: 'POST', 
    contentType: 'application/json; charset=utf-8', 
    data: JSON.stringify(jsonData), 
    dataType: 'jsonp', 
    success: function(responseData, textStatus, jqXHR) { 
     if (responseData.result == "true") {    
      $.mobile.changePage("#registersuccess",{transition:"slide"}); 
     } else { 
      alert("kayıt başarısız"); 
     } 
    } 
}); 

我用Explorer开发人员工具进行监视。我收到此错误消息:

HTTP400:BAD REQUEST - 由于语法无效,请求无法由服务器处理。
GET - HTTP:为MyService回调= jQuery111306711937631005869_1470230696599 & [{ “Name”: “”, “phoneNumber的”: “”, “密码”: “”}] & _ = 1470230696600

这是什么意思:&_=1470230696600

回答

0

datatypejsonp更换为json。 你可以阅读更多关于jsonjsonp这里What are the differences between JSON and JSONP?

+0

当我从JSON更改为JSONP,我采取跨域错误。 –

+0

您可以指定客户端和服务器是否在同一个域上? 如果他们不是,你应该考虑在你的服务器上启用CORS。 – Poly

+0

不,我的服务器和客户端不一样的域。我正在使用亚马逊Linux服务器,我如何启用,除了我添加了我的jquery ajax代码缓存:true;我得到了这个错误信息:GET - http:MyService?callback = jQuery111306711937631005869_1470230696599&[{“name”:“”,“phoneNumber”:“”,“password”:“”}] –

0

见jQuery的文档我解决问题添加服务器站点代码,

public class CORSFilter 

实现ContainerResponseFilter {

public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) 
     throws IOException { 

    MultivaluedMap<String, Object> headers = responseContext.getHeaders(); 

    headers.add("Access-Control-Allow-Origin", "*"); 
    //headers.add("Access-Control-Allow-Origin", "http://podcastpedia.org"); //allows CORS requests only coming from podcastpedia.org  
    headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");   
    headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia"); 
} 

}