简体   繁体   English

JavaScript Ajax请求无法在Firefox和Google Chrome中运行,但在Safari中可以

[英]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.

相关问题 jQuery ajax函数在Safari中不起作用(Firefox,Chrome,IE可以) - jQuery ajax function not working in Safari (Firefox, Chrome, IE okay) Ajax 调用/javascript - 在 IE、Firefox 和 Safari 上运行良好,但在 Chrome 上运行不正常 - Ajax call/javascript - working fine on IE, Firefox and Safari but not on Chrome Javascript在Safari中运行,但不适用于Chrome或Firefox - Javascript working in Safari but not Chrome or Firefox Ajax请求无法在Safari,Chrome中运行 - Ajax request not working in Safari, Chrome ajax 在 chrome 上工作但不能在 firefox 和 safari 上工作 - ajax post working on chrome but not working on firefox and safari 杰森要求在野生动物园工作,但不是chrome / firefox - Json request working on safari but not chrome/firefox Javascript未在Firefox上执行,适用于Chrome和Safari - Javascript not executed on Firefox, working on Chrome & Safari Javascript无法在浏览器中运行? (Firefox,Chrome和Safari) - Javascript not working in browser? (Firefox, Chrome and Safari) For循环中的Javas If语句可在Chrome和Safari中运行,但不能在Firefox中运行 - Javascript If statement in a For loop working in Chrome and Safari but not Firefox Ajax脚本仅适用于Firefox(不适用于Chrome,Safari,Edge等) - Ajax script only working in Firefox (not working in Chrome, Safari, Edge,…)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM