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

前端 未结 2 411
情深已故
情深已故 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
    });
    

提交回复
热议问题