“not well-formed” warning when loading client-side JSON in Firefox via jQuery.ajax

前端 未结 2 407
情深已故
情深已故 2020-12-03 11:14

I am using jQuery\'s ajax method to acquire a static JSON file. The data is loaded from the local file system, hence there is no server, so I can\'t change the MIME type.

相关标签:
2条回答
  • 2020-12-03 12:01

    Sometimes using an HTTP server is not an option, which may mean that MIME types won't be automatically provided for some files. Adapted from Peter Hoffman's answer for jQuery .getJSON Firefox 3 Syntax Error Undefined, use this code before you make any $.getJSON() calls:

    $.ajaxSetup({beforeSend: function(xhr){
      if (xhr.overrideMimeType)
      {
        xhr.overrideMimeType("application/json");
      }
    }
    });
    

    Or, if you're using $.ajax():

    $.ajax({
      url: url,
      beforeSend: function(xhr){
        if (xhr.overrideMimeType)
        {
          xhr.overrideMimeType("application/json");
        }
      },
      dataType: 'json',
      data: data,
      success: callback
    });
    
    0 讨论(0)
  • 2020-12-03 12:10

    Local files and scripting don't mix. Way too much browser security stuff and other weirdness involved. If you want to test things, you should run your stuff through a HTTP server. Installing one locally might be a good idea.

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