简体   繁体   English

JavaScript POST请求问题

[英]JavaScript POST Request Issue

So I'm creating a mobile application using Intel XDK, I am sending a JavaScript POST request of a username and password. 所以我正在使用英特尔XDK创建移动应用程序,我发送了一个用户名和密码的JavaScript POST请求。 I also did some research on the HTTP status codes and found this: 我还对HTTP状态代码做了一些研究,发现了这个:

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: 所以我假设当通过POST请求插入新用户时状态为201.但是当我有这个代码时:

        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. 所以我想知道即使我发送一个POST请求插入数据库,状态如何200。

Secondly, it alerts the "User created." 其次,它提醒“用户创建”。 4 times? 4次? Is that because it is in the function onreadystatechange so it changes each time and is alerted? 是因为它是在onreadystatechange函数中,所以它每次都会改变并被警告? 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中包含if语句,如下所示:

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

Very few websites use those headers, your back-end probably just sends a 200 even though the request was successful in inserting data. 很少有网站使用这些标题,即使请求成功插入数据,您的后端也可能只发送200

About your second question, the reason your alert is triggered four times is because onreadystatechanged is called four times, each with a different readyState : 关于你的第二个问题,你的警报被触发四次的原因是因为onreadystatechanged被调用了四次,每个都有一个不同的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: 所以你可能想在你的if语句中添加XMLHTTP.readyState === 4 ,这样它最终会变成:

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

The status returned is based on how the server decides to handle it, in most cased you will simply get a success (200), so there is no issue or abnormality with what you have done. 返回的状态取决于服务器决定如何处理它,在大多数情况下,您只需获得成功(200),因此您所做的事情没有任何问题或异常。

setInterval = hack, avoid at all costs unless implementing some actual interval function. setInterval = hack,除非实现一些实际的间隔函数,否则不惜一切代价。 You can check the the readystate of the XMLHTTP request with 您可以使用检查XMLHTTP请求的readystate

XMLHTTP.readyState == 4

to filter out only the completed event. 过滤掉已完成的事件。

Here is a list of the ready state events: http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp 以下是准备状态事件的列表: http//www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM