jQuery $ .ajax调用适用于Chrome,但不适用于任何其他浏览器

时间:2022-11-19 09:40:17

The following call works perfectly in Chrome, but fails in every other browser.

以下调用在Chrome中完美运行,但在其他所有浏览器中都失败。

function getInfo(r,c,f){
  return $.parseJSON($.ajax({
      url: baseURL + 'somethingAPI/getInfo',
      data: {
        "data_r": r,
        "data_c": c,
        "data_f": f
      },
      //success: function(data){},
      dataType: "json",
      async: FALSE
    }).response);
}

Yes, I'm using a synchronous ajax call and I believe it is necessary as I don't want any of the other JS to run without this executing and returning data. Although, I'm not entirely sure if something else should be happening with the success callback.

是的,我正在使用同步ajax调用,我认为这是必要的,因为我不希望任何其他JS在没有执行和返回数据的情况下运行。虽然,我不完全确定成功回调是否应该发生其他事情。

Anyway, in Chrome I get the response object (JSON) and can access the data within just fine.

无论如何,在Chrome中我得到了响应对象(JSON)并且可以很好地访问数据。

Does anyone know what I'm doing wrong?

有谁知道我做错了什么?

2 个解决方案

#1


1  

Regarding your point about not knowing how to avoid async: false, is this something like what you're looking to accomplish?

关于你不知道如何避免异步的观点:假,这是你想要完成的事情吗?

function getInfo(r, c, f, callback) {
  $.ajax({
    url: baseURL + 'somethingAPI/getInfo',
    data: {
      "data_r": r,
      "data_c": c,
      "data_f": f
    },
    dataType: "json",
    success: callback,
  });
}

getInfo('foo', 'bar', 'baz', function(response) {
  console.log(response);
});

#2


0  

Rather than parsingJson on the ajax query, here's the syntax I use to conquer these challenges

而不是在ajax查询上解析json,这是我用来克服这些挑战的语法

    $.ajax({
       url: "pagegoeshere.php",                   
       timeout: 30000,
       type: "POST",
       data: 'data1='+data1+'&data2='+data2,
       dataType: 'json',
       error: function(XMLHttpRequest, textStatus, errorThrown)  {
         alert("An error has occurred making the request: " + errorThrown)
       },
       success: function(returnjson){                                                                                                   
           var returnstuff = returnjson.returnstuff;
           //Do next Javascript step here
       }
});

You can run ensuing javascript/jquery in the success and "stack" events together on success of your Ajax call. That way, if it works, it proceeds. Otherwise, the error handling can occur in the provided error section in a manner that you define. I generally fire my ajax calls on a click handler, but it's certainly doable to run it in a function as you have chosen. Be sure to check your return JSON (could be mailed from your processing page, for example) to make sure it's valid JSON. Jsonlint is your friend!

您可以在成功运行随后的javascript / jquery,并在Ajax调用成功时“堆叠”事件。这样,如果它有效,它就会继续。否则,错误处理可以以您定义的方式在提供的错误部分中进行。我通常在单击处理程序上激活我的ajax调用,但是在你选择的函数中运行它肯定是可行的。请务必检查您的返回JSON(例如,可以从您的处理页面邮寄)以确保它是有效的JSON。 Jsonlint是你的朋友!

I've had chrome effectively parse bad HTML and JSON while the other browsers don't on several occasions. I'd suspect it's something along those lines that's specifically causing your issues.

我有chrome有效地解析了糟糕的HTML和JSON,而其他浏览器在几次没有。我怀疑它是那些特别导致你的问题的东西。

#1


1  

Regarding your point about not knowing how to avoid async: false, is this something like what you're looking to accomplish?

关于你不知道如何避免异步的观点:假,这是你想要完成的事情吗?

function getInfo(r, c, f, callback) {
  $.ajax({
    url: baseURL + 'somethingAPI/getInfo',
    data: {
      "data_r": r,
      "data_c": c,
      "data_f": f
    },
    dataType: "json",
    success: callback,
  });
}

getInfo('foo', 'bar', 'baz', function(response) {
  console.log(response);
});

#2


0  

Rather than parsingJson on the ajax query, here's the syntax I use to conquer these challenges

而不是在ajax查询上解析json,这是我用来克服这些挑战的语法

    $.ajax({
       url: "pagegoeshere.php",                   
       timeout: 30000,
       type: "POST",
       data: 'data1='+data1+'&data2='+data2,
       dataType: 'json',
       error: function(XMLHttpRequest, textStatus, errorThrown)  {
         alert("An error has occurred making the request: " + errorThrown)
       },
       success: function(returnjson){                                                                                                   
           var returnstuff = returnjson.returnstuff;
           //Do next Javascript step here
       }
});

You can run ensuing javascript/jquery in the success and "stack" events together on success of your Ajax call. That way, if it works, it proceeds. Otherwise, the error handling can occur in the provided error section in a manner that you define. I generally fire my ajax calls on a click handler, but it's certainly doable to run it in a function as you have chosen. Be sure to check your return JSON (could be mailed from your processing page, for example) to make sure it's valid JSON. Jsonlint is your friend!

您可以在成功运行随后的javascript / jquery,并在Ajax调用成功时“堆叠”事件。这样,如果它有效,它就会继续。否则,错误处理可以以您定义的方式在提供的错误部分中进行。我通常在单击处理程序上激活我的ajax调用,但是在你选择的函数中运行它肯定是可行的。请务必检查您的返回JSON(例如,可以从您的处理页面邮寄)以确保它是有效的JSON。 Jsonlint是你的朋友!

I've had chrome effectively parse bad HTML and JSON while the other browsers don't on several occasions. I'd suspect it's something along those lines that's specifically causing your issues.

我有chrome有效地解析了糟糕的HTML和JSON,而其他浏览器在几次没有。我怀疑它是那些特别导致你的问题的东西。