jQuery Ajax call No element found issue
I am getting a javascript error on firefox 3.5, when trying to call an ajax method.
Please find the error below:
XML Pa开发者_StackOverflow中文版rsing Error: no element found Location: moz-nullprincipal:{1a2c8133-f48f-4707-90f3-1a2b2f2d62e2} Line Number 1, Column 1:
^
this is my javascript function:
function Update(Id) {
$.ajax({
type: "GET",
url: ROOT_URL + "/sevice/udates.svc/Update?Id=" + Id,
success: function(response) {
},
async: false
});
}
I fixed the problem by setting mimeType to "text/html"
The ajax call expects XML back (perhaps due to bad guessing) and tries to parse it and fails if nothing is returned or it is not valid XML..
Use the dataType
option to specify the format of the response.
From the comments it looks like some browsers cannot handle an no-content response. So, a workaround for such cases might be to return something from your service (even a single space).
I've come across an alternative cause of this - might help someone.
If you make a $.ajax
request (in my case a PUT
request) that returns a 200 header but no body content I've seen this same XML Parsing error message occur - even when the dataType
is set to json
.
(At least) two solutions work:
- Make all API
PUT
requests return some content, or - Return a 204 'No Content' header instead (what I ended up doing)
It's a known FireFox bug. https://bugzilla.mozilla.org/show_bug.cgi?id=547718 to quick fix this , you maybe can return an response with html structure(but no content).
async
is also part of options. Also specify the dataType
as xml
function Update(Id) {
$.ajax({
type: "GET",
async: false,
dataType: "XML",
url: ROOT_URL + "/sevice/udates.svc/Update?Id=" + Id,
success: function(response) {
}
});
}
You need to send html document to the output (the output udates.svc in your case) . If you use ASP.NET, you could do the following:
Response.Clear();
Response.Write("<html xmlns=”http://www.w3.org/1999/xhtml”>");
Response.Write("<head><title></title></head>");
Response.Write("<body>");
Response.Write("your output");
Response.Write("</body>");
Response.Write("</html>");
Response.ContentType = "text/HTML";
Response.End();
精彩评论