【问题标题】:jQuery Ajax POST Request - Response Text empty ans status is 400 Bad Request in FirefoxjQuery Ajax POST 请求 - 响应文本为空 ans 状态为 400 Firefox 中的错误请求
【发布时间】:2013-10-24 10:15:21
【问题描述】:

我有以下 jQuery AJAX 请求:

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 中收到的输出。

响应标头:

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: 

我是 jquery 的新手,所以基本上不知道我在哪里做错了。有一件事是肯定它会出错的ajax调用的一部分,但为什么呢?它能够形成json ...那为什么它会出错?请帮帮我。

【问题讨论】:

    标签: jquery ajax json post


    【解决方案1】:

    来自jquery doc
    当您输入dataType=json 时,表示您不应返回空内容。

    “json”:将响应评估为 JSON 并返回一个 JavaScript 对象。 JSON数据被严格解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。从 jQuery 1.9 开始,空响应也会被拒绝;服务器应该返回 null 或 {} 的响应。 (有关正确的 JSON 格式的更多信息,请参阅 json.org。)

    此外,您需要检查您的响应状态是 200 而不是 400。

    您的服务器端代码有问题。

    @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();
    }
    

    【讨论】:

    • 您好,感谢您的回复。抱歉,我忘了补充说我收到的响应状态为 400 Bad Request,响应为空。
    • 对不起,你没有提到你的服务器相关代码。但是您是否尝试过上面的建议,将空响应更改为 null{}
    • 我有这个发布方法:@POST @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.TEXT_PLAIN}) @Path("/hello") public Response create(String name) throws JSONException{ System.out.println("创建账户记录"); JSONObject jsonObj = new JSONObject(name); return Response.status(201).entity(jsonObj).build();} 我收到 500 内部错误,并且 firebug 中的响应选项卡为空白。请让我知道发送 post 方法的响应的正确方法是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2019-09-12
    • 2015-06-12
    相关资源
    最近更新 更多