简体   繁体   English

XMLHttpRequest Post方法-标头正在停止功能

[英]XMLHttpRequest Post Method - Headers are stopping function

I'm trying to send an XMLHttpRequest object to my Rails server but the headers are causing my function to stop. 我正在尝试将XMLHttpRequest对象发送到我的Rails服务器,但是标题导致我的功能停止。 Here are my observations: 这是我的观察结果:

  1. When I comment out the 3 lines of code that set the headers, then xhr.readyState will eventually equal 4 (alert boxes within the anonymous function fire off). 当我注释掉设置标头的3行代码时,xhr.readyState最终将等于4(关闭匿名函数中的警报框)。

  2. If any one of the 3 header lines are uncommented, then the xhr object never changes state (none of the alert boxes ever fire off). 如果3条标题行中的任何一条未注释,则xhr对象将永远不会更改状态(不会触发任何警报框)。

     function saveUserProfile(){ var user_email = $('#userEmail_box').val(); var xhr = new XMLHttpRequest(); xhr.onreadystatechange=function(){ if (xhr.readyState==4 && xhr.status==200) { alert("Yes: " + xhr.readyState); } alert("No: " + xhr.readyState); } var method = 'POST'; var params = 'userEmail=user_email'; var url = 'http://localhost:3000/xhr_requests.json'; var async = true; //Need to send proper header information with POST request xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.setRequestHeader('Content-length', params.length); xhr.setRequestHeader('Connection', 'close'); xhr.open(method, url, async); xhr.send(params); } 

My three questions are: 我的三个问题是:

  1. What do I need to change in the code above in order to send data through the POST method? 为了通过POST方法发送数据,我需要在上面的代码中进行哪些更改?

  2. I'm under the impression that the POST method requires some headers to be sent but am not clear about which ones though "xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');" 我的印象是POST方法需要发送一些标头,但不清楚“ xhr.setRequestHeader('Content-type','application / x-www-form-urlencoded');”中的哪个标头。 seems to be one that is often mentioned in references. 似乎是参考文献中经常提到的一种。 Can somebody help me understand a) why headers need to be sent and b) which ones need to be sent? 有人可以帮助我理解a)为什么需要发送标头,以及b)需要发送哪些标头吗?

  3. I'm using a rails server and am developing locally. 我正在使用Rails服务器,并且正在本地开发。 Ultimately, this code will be executed on the client side of a mobile device which will go to a hosted rails server for passing and receiving data. 最终,此代码将在移动设备的客户端上执行,该设备将转到托管的Rails服务器以传递和接收数据。 Are there limitations with using the POST method with a rails server? 在rails服务器上使用POST方法是否有限制? Keep in mind that I plan to use JSON when sending information to the client from the server. 请记住,我打算在从服务器向客户端发送信息时使用JSON。

Thanks! 谢谢!

UPDATE: The headers should come AFTER the opening the xhr request but BEFORE sending it: 更新:标头应该在打开xhr请求之后出现,但在发送之前:

    xhr.open(method, url, async);        

    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Content-length', params.length);
    xhr.setRequestHeader('Connection', 'close');

    xhr.send(params);

Hope this post saves somebody else 4 hours. 希望这篇文章可以为其他人节省4个小时。

Does your web page with the JavaScript code also live on localhost:3000? 您的带有JavaScript代码的网页是否也位于localhost:3000上? If not, this is considered a cross-domain request, and your server will need to return special headers. 如果不是,则将其视为跨域请求,并且您的服务器将需要返回特殊的标头。 So you have two options: 因此,您有两种选择:

1) Host the web page on the same domain as the server, which will make this a same-domain request. 1)将网页托管在与服务器相同的域中,这将使该请求成为同一域的请求。

2) Have your server return the appropriate CORS headers. 2)让您的服务器返回适当的CORS标头。 You can learn more about CORS here: http://www.html5rocks.com/en/tutorials/cors/ 您可以在此处了解有关CORS的更多信息: http ://www.html5rocks.com/en/tutorials/cors/

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

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