tags:

views:

202

answers:

4

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

$.ajax({
  url:         "programData.txt",
  type:        "GET",
  dataType:    "text",
  cache:       false,
  success:     processData
});

When I make the request, though, I get the following error:

Error: not well-formed Source File: file:///projects/foo/programData.txt?_=1259694590361 Line: 1, Column: 2

Why is jQuery trying to process my plain text and how do I get it to stop?

A: 

Supported types are:

* "xml": Treat the response as an XML document that can be processed via jQuery. 
* "html": Treat the response as HTML (plain text); included script tags are evaluated. 
* "script": Evaluates the response as JavaScript and evaluates it. 
* "json": Evaluates the response as JSON and sends a JavaScript Object to the success callback.

Perhaps use should use "html" instead of "text" since jQuery seems to not parse anything if you specify html as the type.

Mark Byers
http://docs.jquery.com/Ajax/jQuery.ajax#options seems to list more dataTypes than that, although I can't speak to older versions of jQuery
Daniel LeCheminant
+3  A: 

Are you loading up the page in your browser over HTTP or just opening it as a regular file? Does the address of the page running the javascript begin with http: or file: ? I suspect it is the latter, and Ajax calls work differently in that situation (see response by tvanfonsson below). If you're building a web app that will be served over HTTP later, try running your page using a local HTTP server.

Jaanus
Indeed, I was trying it over `file://` access. Moving to Apache fixed the problem.
James A. Rosen
+1  A: 

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

tvanfosson
A: 

jQuery doesn't seem to have this issue on chrome and ie8.

Dave.Sol