[英]JavaScript Ajax request not working in Firefox and Google Chrome, but it is okay in Safari
I'm using some JavaScript to send an Ajax request to an Arduino webserver and change the HTML on a webpage. 我正在使用一些JavaScript将Ajax请求发送到Arduino网络服务器并更改网页上的HTML 。
In Safari this has been working great, but when I try to load it in Firefox and Google Chrome the document elements never update. 在Safari中,这一直很好,但是当我尝试在Firefox和Google Chrome中加载它时,文档元素永远不会更新。 In the debugger consoles I can see the requests and responses coming back so I'm guessing that there is an issue with parsing the response to an array?
在调试器控制台中,我可以看到返回的请求和响应,因此我猜测将响应解析为数组有问题吗?
Here is the code: 这是代码:
function GetSwitchState()
{
nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
var response = this.responseText;
var comma = ",";
var inputArray = response.split(comma);
var green = inputArray[0];
var red = inputArray[1];
var fault = inputArray[2];
var counter = inputArray[3];
document.getElementById('green').innerHTML = green;
document.getElementById("red").innerHTML = red;
document.getElementById("status").innerHTML = fault;
document.getElementById("cars").innerHTML = counter;
}
}
}
}
request.open("GET", "url" + nocache, true);
request.send(null);
setTimeout('GetSwitchState()', 1000);
}
The response from the Arduino webserver is four comma-separated values. Arduino网络服务器的响应是四个逗号分隔的值。
What I did today was pretty much the same! 我今天所做的几乎是一样的!
When I ran an Ajax request to a PHP file and wanted to return an array I needed to specify the return-datatype as "json". 当我向PHP文件运行Ajax请求并想返回一个数组时,我需要将return-datatype指定为“ json”。 In my PHP file I then returned my values like this:
然后,在我的PHP文件中,我返回了如下值:
return json_encode(array(
'success' => false,
'error' => $_POST['password_hashed']
));
I was acctually using jQuery to run the request. 我使用jQuery来运行请求。 That looks like this:
看起来像这样:
$.ajax({
type: 'POST',
url: 'script.php',
data: 'password_hashed=' + hex_sha512(str_password) + '&email=' + str_email, //Clientside password hashing
cache: false,
dataType: 'json',
success: function(value){
//Ajax successfully ran
alert(value.success + '_' + value.error); //=false_[hash]
},
error: function(){
//Ajax error occured -> Display error message in specified element
alert('error with request');
}
});
I just started with Ajax two days ago, and this may not help a lot, but it is worth trying. 我是两天前刚开始使用Ajax的,这可能无济于事,但是值得尝试。
Okay it looks like the issue was actually getting past the 好的,看起来问题实际上已经解决了
{
if (this.readyState == 4) {
if (this.status == 200) {
arguments. 论点。 When I changed it to:
当我将其更改为:
{
if(response.readState == 4) {
I was able to move past that statement in firefox. 我能够在Firefox中跳过该声明。 To get the status to 200 instead of 0 I needed to modify the response header on the arduino side to include:
为了使状态变为200而不是0,我需要修改arduino端的响应头,使其包括:
Access-Control-Allow-Origin: *
To allow Cross Origin Domain Requests in FireFox. 在FireFox中允许跨源域请求。 Once I made these changes the code works great, I guess I was barking up the wrong tree with my array assumption.
一旦进行了这些更改,代码就可以很好地工作了,我想我在数组假设的基础上错了树。 Thanks for the help!
谢谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.