简体   繁体   English

Ajax脚本仅适用于Firefox(不适用于Chrome,Safari,Edge等)

[英]Ajax script only working in Firefox (not working in Chrome, Safari, Edge,…)

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) . 该代码在Firefox中工作正常,但是在我测试过的所有其他浏览器中, if (this.readyState == 4 && this.status == 200)出现错误“ SyntaxError:JSON Parse error:Unexpected EOF”。 The json-string looks like this: 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-Code: 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);
});

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. 我已经简化了您的代码,并更改了外部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));
          });
        });
      }
    });

});

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

相关问题 ajax 在 chrome 上工作但不能在 firefox 和 safari 上工作 - ajax post working on chrome but not working on firefox and safari 代码在chrome,firefox,mircrosoft edge中正常工作,但在safari中不工作 - code working good in chrome, firefox, mircrosoft edge but not working in safari Ajax在Safari和Chrome中不起作用 - ajax is not working in safari and chrome JavaScript Ajax请求无法在Firefox和Google Chrome中运行,但在Safari中可以 - JavaScript Ajax request not working in Firefox and Google Chrome, but it is okay in Safari 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 退出仅适用于 Edge 而不是 Chrome 和 Firefox 的页面的消息 - Message for quitting a page only working with Edge and not Chrome and Firefox 使用Firefox而不使用Chrome,Safari的代码 - Code Working with Firefox but not with Chrome, Safari Javascript在Safari中运行,但不适用于Chrome或Firefox - Javascript working in Safari but not Chrome or Firefox onFocus在Chrome,Firefox,Safari中不起作用 - onFocus not working in Chrome, Firefox, Safari
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM