Ajax脚本仅适用于Firefox(不适用于Chrome,Safari,Edge等) [英] Ajax script only working in Firefox (not working in Chrome, Safari, Edge,...)

查看:142
本文介绍了Ajax脚本仅适用于Firefox(不适用于Chrome,Safari,Edge等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在Firefox中可以正常工作,但是在我测试过的所有其他浏览器中,我在行if (this.readyState == 4 && this.status == 200)中收到错误"SyntaxError:JSON解析错误:意外的EOF". json-string看起来像这样:

[{"ID":"1","token":"1234","name":"Test Satio","matno":"11111","reg_date":"2017-10-24 00:00:00","last_active":"2017-10-24 00:00:00","user_id":"25"},{"ID":"3","token":"2232434","name":"Test Satio 2","matno":"44444","reg_date":"2017-10-23 00:00:00","last_active":"0000-00-00 00:00:00","user_id":"25"},{"ID":"5","token":"32233","name":"Test Satio 3","matno":"12","reg_date":"0000-00-00 00:00:00","last_active":"0000-00-00 00:00:00","user_id":"25"}]

JS代码:

 $(document).ready(function postData() {
    var id = localStorage.getItem('user-id');
    var token = localStorage.getItem('user-token');
    var vars = "id=" + id + "&token=" + token;
    var hr = new XMLHttpRequest();
    var url = "../php/getUsers.php";

      hr.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {  //in this line I get the error
            var data = JSON.parse(hr.responseText);

            for(var i=0;i<data.length;i++){

                    var templateData = {
                        name: data[i].name,
                        id: data[i].id

                    };
                  var id=templateData['id'];

                  $.get('templates/user.htm', (function (templateData) {
                     return function(templates) {
                        var template = $(templates).filter('#user-template').html();
                        $('#user-container').append(Mustache.render(template,    templateData));
                      }
                  })(templateData));

             }
        }
    }
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.send(vars);
});

要使此代码在所有浏览器中都能正常工作,我需要更改什么?

谢谢卢卡斯

解决方案

我认为您无需为每个用户触发对用户模板的请求,我认为您只需执行一次并保存. >

我已经稍微简化了代码,并更改了外部ajax以使用jquery.

 $(document).ready(function postData() {

    $.post({
      url: "../php/getUsers.php",
      data: {
        id: localStorage.getItem('user-id'),
        token: localStorage.getItem('user-token')
      },
      dataType: 'JSON',
      success: function (data) {
        // now get the template
        $.get('templates/user.htm', function (templates) {
          var template = $(templates).filter('#user-template').html();
          // we have the template and userdata array
          data.map(function (el) {
            return {id: el.id, name: el.name};
          }).forEach(function (templateData) {
             $('#user-container').append(Mustache.render(template, templateData));
          });
        });
      }
    });

});

this code is working fine in Firefox, but in all other browsers I have tested I get the error "SyntaxError: JSON Parse error: Unexpected EOF" in line if (this.readyState == 4 && this.status == 200). The json-string looks like this:

[{"ID":"1","token":"1234","name":"Test Satio","matno":"11111","reg_date":"2017-10-24 00:00:00","last_active":"2017-10-24 00:00:00","user_id":"25"},{"ID":"3","token":"2232434","name":"Test Satio 2","matno":"44444","reg_date":"2017-10-23 00:00:00","last_active":"0000-00-00 00:00:00","user_id":"25"},{"ID":"5","token":"32233","name":"Test Satio 3","matno":"12","reg_date":"0000-00-00 00:00:00","last_active":"0000-00-00 00:00:00","user_id":"25"}]

JS-Code:

 $(document).ready(function postData() {
    var id = localStorage.getItem('user-id');
    var token = localStorage.getItem('user-token');
    var vars = "id=" + id + "&token=" + token;
    var hr = new XMLHttpRequest();
    var url = "../php/getUsers.php";

      hr.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {  //in this line I get the error
            var data = JSON.parse(hr.responseText);

            for(var i=0;i<data.length;i++){

                    var templateData = {
                        name: data[i].name,
                        id: data[i].id

                    };
                  var id=templateData['id'];

                  $.get('templates/user.htm', (function (templateData) {
                     return function(templates) {
                        var template = $(templates).filter('#user-template').html();
                        $('#user-container').append(Mustache.render(template,    templateData));
                      }
                  })(templateData));

             }
        }
    }
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.send(vars);
});

What do I need to change to get this code working in all browsers?

Thanks, Lukas

解决方案

I don't think you need to fire off a request for the user template for each user, I think you can do it once and save it.

I've simplified your code a bit and changed your outer ajax to use jquery.

 $(document).ready(function postData() {

    $.post({
      url: "../php/getUsers.php",
      data: {
        id: localStorage.getItem('user-id'),
        token: localStorage.getItem('user-token')
      },
      dataType: 'JSON',
      success: function (data) {
        // now get the template
        $.get('templates/user.htm', function (templates) {
          var template = $(templates).filter('#user-template').html();
          // we have the template and userdata array
          data.map(function (el) {
            return {id: el.id, name: el.name};
          }).forEach(function (templateData) {
             $('#user-container').append(Mustache.render(template, templateData));
          });
        });
      }
    });

});

这篇关于Ajax脚本仅适用于Firefox(不适用于Chrome,Safari,Edge等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆