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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭