jQuery Ajax POST请求-响应文本空ans状态在Firefox中为400错误请求
我有以下jQuery AJAX请求:jQuery Ajax POST请求-响应文本空ans状态在Firefox中为400错误请求,jquery,ajax,json,post,Jquery,Ajax,Json,Post,我有以下jQuery AJAX请求: function addRecord() { console.log('addRecord'); $.ajax({ type: 'POST', url: rootURL, contentType: 'application/json', dataType: "json", data: formToJSON(),
function addRecord() {
console.log('addRecord');
$.ajax({
type: 'POST',
url: rootURL,
contentType: 'application/json',
dataType: "json",
data: formToJSON(),
success: function(data, textStatus, jqXHR){
alert('form submitted successfully');
alert('data'+data);
},
error: function (xhRequest, ErrorText, thrownError) {
alert("Failed to process request correctly, please try again");
console.log('xhRequest: ' + xhRequest + "\n");
console.log('ErrorText: ' + ErrorText + "\n");
console.log('thrownError: ' + thrownError + "\n");
}
});
}
function formToJSON() {
return JSON.stringify({"dateofVisit": $('#dateofVisit').val()});
}
以下是我在firebug中收到的输出
响应标题:
职位:
答复:
控制台输出:
我是jquery新手,所以基本上不知道我在哪里犯了错误。有一件事是肯定的,它会在ajax调用中出错,但为什么呢?它能够形成json…那么为什么会出现错误呢?请帮帮我。来自当您放置
dataType=json
时,这意味着您不应该返回空内容
“json”:将响应计算为json并返回一个JavaScript对象。JSON数据被严格解析;任何格式错误的JSON都将被拒绝,并引发解析错误。从jQuery1.9开始,空响应也被拒绝;服务器应该返回null或{}响应。(有关正确json格式的更多信息,请参见json.org。)
另外,您需要检查您的响应状态是否为200而不是400
您的服务器端代码有一些问题。嗨,谢谢你的回复。抱歉,我忘记添加,我得到的响应状态为400错误请求,响应为空。抱歉,您没有提到与服务器相关的代码。但是您尝试过上面的建议吗,将空响应改为
null
或{}
?我有这个post方法:@post@Consumes({MediaType.APPLICATION\u JSON})@products({MediaType.TEXT\u PLAIN})@Path(“/hello”)公共响应创建(字符串名)抛出JSONException{System.out.println(“正在为帐户创建记录”);JSONObject jsonObj=new JSONObject(名称);返回Response.status(201).entity(jsonObj.build();}我收到500个内部错误,firebug中的Response选项卡为空。请告诉我发送post方法响应的正确方法。
Server Apache-Coyote/1.1
Access-Control-Allow-Orig... *
Access-Control-Allow-Cred... true
Access-Control-Allow-Meth... GET, POST, DELETE, PUT, OPTIONS, HEAD
Access-Control-Allow-Head... Content-Type, Accept, X-Requested-With
Content-Type text/plain
Transfer-Encoding chunked
Date Thu, 24 Oct 2013 09:37:34 GMT
Connection close
Request Headersview source
Host localhost:8080
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/json; charset=UTF-8
Referer http://mydomain.com/DemoPurpose/demo.html
Content-Length 28
Origin http://mydomain.com
Pragma no-cache
Cache-Control no-cache
JSON
dateofVisit
"23-10-2013"
Source
{"dateofVisit":"23-10-2013"}
empty
xhRequest: [object Object]
ErrorText: error
thrownError:
@POST @Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN}) @Path("/hello")
//---------------------^^^ why you use plain text? why not application json
public Response create(String name) throws JSONException
{
System.out.println("creating record for account");
//JSONObject jsonObj = new JSONObject(name);
//------------------------------^^^^^ it's not jsonformat, error should be here
//try this way
JSONObject jsonObj = new JSONObject();
jsonObj.append("name",name);
return Response.status(201).entity(jsonObj).build();
}