XMLHttpRequest Post 方法 - 标头正在停止功能
我正在尝试将 XMLHttpRequest 对象发送到我的 Rails 服务器,但标头导致我的函数停止。以下是我的观察结果:
当我注释掉设置标头的 3 行代码时,xhr.readyState 最终将等于 4(匿名函数内的警报框将关闭)。
如果 3 个标题行中的任何一个未注释,则 xhr 对象永远不会更改状态(没有任何警报框会触发)。
函数 saveUserProfile(){ var user_email = $('#userEmail_box').val(); var xhr = new XMLHttpRequest(); xhr.onreadystatechange=函数(){ if (xhr.readyState==4 && xhr.status==200) { 警报(“是:”+ xhr.readyState); } 警报(“否:”+ xhr.readyState); } var 方法 = 'POST'; var params = 'userEmail=user_email'; var url = 'http://localhost:3000/xhr_requests.json'; var 异步 = true; //需要通过POST请求发送正确的头信息 xhr.setRequestHeader('内容类型', 'application/x-www-form-urlencoded'); xhr.setRequestHeader('内容长度', params.length); xhr.setRequestHeader('连接', '关闭'); xhr.open(方法、url、异步); xhr.send(参数); }
我的三个问题是:
我需要在上面的代码中更改什么才能通过 POST 方法发送数据?
我的印象是 POST 方法需要发送一些标头,但不清楚哪些标头“xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded') ;”似乎是参考文献中经常提到的一个。有人可以帮助我理解 a) 为什么需要发送标头和 b) 需要发送哪些标头?
我正在使用 Rails 服务器并在本地进行开发。最终,此代码将在移动设备的客户端执行,该移动设备将转到托管的 Rails 服务器以传递和接收数据。在 Rails 服务器上使用 POST 方法是否有限制?请记住,我计划在从服务器向客户端发送信息时使用 JSON。
谢谢!
更新:标头应该在打开 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);
希望这篇文章可以为其他人节省 4 个小时。
I'm trying to send an XMLHttpRequest object to my Rails server but the headers are causing my function to stop. Here are my observations:
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).
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).
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:
What do I need to change in the code above in order to send data through the POST method?
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');" 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?
I'm using a rails server and am developing locally. 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. Are there limitations with using the POST method with a rails server? Keep in mind that I plan to use JSON when sending information to the client from the server.
Thanks!
UPDATE: The headers should come AFTER the opening the xhr request but BEFORE sending it:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的包含 JavaScript 代码的网页是否也位于 localhost:3000 上?如果不是,则这被认为是跨域请求,并且您的服务器将需要返回特殊标头。因此,您有两个选择:
1)将网页托管在与服务器相同的域上,这将使该请求成为同域请求。
2) 让您的服务器返回适当的 CORS 标头。您可以在此处了解有关 CORS 的更多信息:http://www.html5rocks.com/en/tutorials/科尔斯/
Does your web page with the JavaScript code also live on 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.
2) Have your server return the appropriate CORS headers. You can learn more about CORS here: http://www.html5rocks.com/en/tutorials/cors/