简体   繁体   English

JavaScript XMLHttpRequest发送失败

[英]JavaScript XMLHttpRequest send fails

I try to make an AJAX requestin Chrome using the following code: 我尝试使用以下代码在Chrome中提出AJAX请求:

var url = "http://" + document.domain + "/status_data.lua?resetsessiontimeout=false";
xmlhttp.open("GET", url, true);
xmlhttp.send(null);

Using WireShark is see that send doesn't send anything... There is no GET emitted. 使用WireShark可以看到send没有发送任何东西...没有发出GET But if I add an & at the end of the URL: 但是,如果我在网址末尾添加&

var url = "http://" + document.domain + "/status_data.lua?resetsessiontimeout=false&";
                                                                                   ^

then send will emit the GET request. 然后send将发出GET请求。

Is this expected? 这是预期的吗?

I'm going to guess a couple of common errors people (and I) have made. 我将猜测人们(和我)所犯的几个常见错误。

  1. Did you create an instance of xmlhttp? 您是否创建了xmlhttp实例? In otherwords, do you have a line before your code above that has "var xmlhttp = new XMLHttpRequest();" 换句话说,在上面的代码之前,您是否有一行内容为“ var xmlhttp = new XMLHttpRequest();” (case sensitive) (区分大小写)

  2. Are you testing this through a file (like C:/test.html) or on a webserver (like Apache or jboss)? 您是通过文件(例如C:/test.html)还是在网络服务器(例如Apache或jboss)上对此进行测试? If you do the former, you'll get a Cross Point Origin error and doing the latter will fix it. 如果您使用前者,则会收到“交叉点原点”错误,而后者会解决该问题。

  3. Are you sure the server code is not looking for additional variables after resetsessiontimeout? 您确定在resetsessiontimeout之后服务器代码没有在寻找其他变量吗? Because the '&' is used to delimit additional variables. 因为“&”用于定界其他变量。 Although I'm pretty sure it was unneeded to end a variable. 尽管我很确定不需要结束变量。

Like if I wanted to send an error string with that url. 就像我想发送带有该URL的错误字符串一样。 I can do resetsessiontimeout=false&errmsg=test 我可以做resetsessiontimeout = false&errmsg = test

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

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