jQuery $.ajax Not Working in IE8 but it works on FireFox & Chrome

前端 未结 6 2110
臣服心动
臣服心动 2020-12-17 01:58

I have the following ajax call which works perfectly in Firefox and Chrome but not IE:

function getAJAXdates( startDate, numberOfNights, opts ) {

    var mo         


        
相关标签:
6条回答
  • 2020-12-17 02:26

    I would comment out the contentType and add dataType: "json"

    from http://api.jquery.com/jQuery.ajax/

    dataType: The type of data that you're expecting back from the server.

    contentType: When sending data to the server, use this content-type.

    you are specifying that you are sending json, but you are not - maybe this is the issue?

    0 讨论(0)
  • 2020-12-17 02:29

    I also have encountered a somewhat similar problem with $.ajax() (jquery v1.4.2). It's not working in IE8, whereas in Firefox it's working.

    However, I've noticed from the IE8 debug toolbar that my page is in quirks mode. So, I forcefully make it to work in standard mode by inserting this doctype <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">. Suddenly the $.ajax() works!

    I don't really understand about quirks/standard mode but the word "standard" somehow feels closer to Firefox or Chrome, rather than IE. So that's how I got the idea.

    @see http://en.wikipedia.org/wiki/Quirks_mode

    0 讨论(0)
  • 2020-12-17 02:29

    Check if your page just returns OK or if it returns 'OK'. Only 'OK' is valid JSON. Use a tool like JSONLint to check the value that comes from the request.

    0 讨论(0)
  • 2020-12-17 02:33

    Most of the time IE-specific parse errors are caused by extra commas. For example, [1, 2, 3,] is valid in FF but not in IE. Anyway, you should paste in the JSON response, it is impossible to tell the problem without that.

    0 讨论(0)
  • 2020-12-17 02:34

    What version of jQuery are you using?

    If you check jquery's code, parsererror is thrown when jQuery.httpData() is called. Here's the code from jquery:

    if ( status === "success" ) {
      // Watch for, and catch, XML document parse errors
      try {
        // process the data (runs the xml through httpData regardless of callback)
        data = jQuery.httpData( xhr, s.dataType, s );
      } catch(err) {
        status = "parsererror";
        errMsg = err;
      }
    }
    

    Maybe jQuery.httpData() is worth looking at. That is, you can check if jQuery.parseJSON is called and if it indeed returns an object.

    if ( typeof data === "string" ) {
      // Get the JavaScript object, if JSON is used.
      if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
        console.log(data); //add this
        data = jQuery.parseJSON( data );
        console.log("data parsed successfully"); //add this
    
    0 讨论(0)
  • 2020-12-17 02:39

    What if you simply type alert(data); or var myObject = eval('(' + data + ')'); ?

    And if you call the function directly from the browser by typing on the url bar your ajax call with all the parameters in "get" (&param1=param1value&param2=...)? You should be able to read the response.

    Something in JSON response is making IE crazy.

    0 讨论(0)
提交回复
热议问题