duanjiongzhen2523 2014-12-23 14:47
浏览 240
已采纳

JavaScript POST请求问题

So I'm creating a mobile application using Intel XDK, I am sending a JavaScript POST request of a username and password. I also did some research on the HTTP status codes and found this:

200 OK - Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request the response will contain an entity describing or containing the result of the action.

201 Created - The request has been fulfilled and resulted in a new resource being created.

202 Accepted - The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place.

So I would assume that when a new user is inserted through a POST request the status would be 201. However when I had this code:

        XMLHTTP.onreadystatechange = function () {
            if (XMLHTTP.status == 201) {
                alert("User created.");
            } else {
                alert("Error!");
            }
        }

It would show the "Error!" and not "User created." But when I added this on to the code:

        XMLHTTP.onreadystatechange = function () {
            if (XMLHTTP.status == 201 || XMLHTTP.status == 200) {
                alert("User created.");
            } else {
                alert("Error!");
            }
        }

It showed "User created." So I was wondering how come the status is 200 even though I'm sending a POST request to insert in to a database.

Secondly, it alerts the "User created." 4 times? Is that because it is in the function onreadystatechange so it changes each time and is alerted? If so I can I make it so that it only alerts one? Should I have an if statement wrapped in a setinterval as shown below:

setInterval(function () {
    if (XMLHTTP.status == StatusNumberHere) {
        alert("Blah");
    }
}, 10);
  • 写回答

2条回答 默认 最新

  • duanjiushu5063 2014-12-23 15:05
    关注

    Very few websites use those headers, your back-end probably just sends a 200 even though the request was successful in inserting data.

    About your second question, the reason your alert is triggered four times is because onreadystatechanged is called four times, each with a different readyState:

    1. Server connection established
    2. request received
    3. processing request
    4. request finished and response is ready

    So you probably want to add XMLHTTP.readyState === 4 to your if statement so it in the end becomes:

        XMLHTTP.onreadystatechange = function () {
            if (XMLHTTP.status === 200 && XMLHTTP.readyState === 4) {
                alert("User created.");
            } else {
                alert("Error!");
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图