Why does jQuery insist my plain text is not “well-formed”?

后端 未结 5 1469
借酒劲吻你
借酒劲吻你 2020-12-06 12:53

I\'m making an AJAX call to retrieve some plain text:

$.ajax({
  url:         \"programData.txt\",
  type:        \"GET\",
  dataType:    \"text\",
  cache:          


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-06 13:39

    You get different response codes when opening a local file via XMLHttpRequest than you do when using an HTTP request. I suspect that since you are opening a a local file, jQuery is choking on the response code, thinking it's an error because it's not 200 OK.

    Reference

    Example: Non-HTTP synchronous request

    Despite its name, XMLHttpRequest can be used for non-HTTP requests. This example shows how to use it to fetch a file from the local file system.

    var req = new XMLHttpRequest();
    req.open('GET', 'file:///home/user/file.json', false); 
    req.send(null);
    if(req.status == 0)
      dump(req.responseText);
    

    The key thing to note here is that the result status is being compared to 0 for success instead of 200. This is because the file and ftp schemes do not use HTTP result codes

提交回复
热议问题