Got "parsererror" when trying to using Ajax call REST Service - ajax

I have been puzzled by this question for almost a week and still not able to get it solved.
The problem is while using JS Ajax calling a REST GET method, but I always got
parser error
However it's working fine for Firefox's REST Client
Check the Code here.

The URL http://wally.pythonanywhere.com/test/ is not producing a valid JSONP. Valid JSONP response should look like your json data wrapped in a function invocation. Visit JSONP doc for more details.
The actual error your code facing is "jQuery18308228800671640784_1374132969407 was not called". This is because of the invalid JSONP response.

Related

What error to return if method expects only ajax calls?

What HTTP status should action return if it expects to only be used via AJAX, but was called without proper ajax headers? I feel I should signify some error, but I can't really find appropriate one.
I guess best would be 405 Method not allowed, but it would be weird if for example ajax GET requests returned content, and plain GET would return 405.
You can fulfill the request, but you refuse to do it since it's not in AJAX, so I think the appropriate error is 403 - forbidden.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4
Anyways, I don't think any error code will be weird if it'll be accompanied with a clear explanation.

How to debug a failed ajax request in google chrome?

I have a web application that crashes on ajax requests with google chrome (it works with every other web browser it was tested it). After debugging I found that the error is caused by response.responseText being undefined. The xhr object looks like this:
argument: undefined
isAbort: false
isTimeout: undefined
status: 0
statusText: "communication failure"
tId: 3
In debugger in the 'network' tab I get "(failed)", however all the headers are there and I can even copy into clipboard the response body (which is a valid JSON).
My question is - how can I debug this problem? Where to find additional information, what causes this request to fail?
I finally found the solution to my problem : AdBlocks, when it blocks an ajax request, it just says "communication failure".
The first thing I would double-check is that the data coming back from the response is valid JSON. Just pass it through a JSON validator like this online JSONLint: http://jsonlint.com/
I assume that you are using something like jQuery to make your AJAX requests. If so, then make sure that you are using the development version of that library. Now that you are using the development version (uncompressed) of the script, find the particular function that you are using (eg. $.ajax) and then, within the Chrome inspector, insert a breakpoint in the code where the AJAX response is first handled (eg. https://github.com/jquery/jquery/blob/master/src/ajax.js#L579). Then proceed to step through the code, inspecting various return-values to see exactly what is going wrong.
If you are not using something like jQuery to make AJAX calls, then I'd recommend using a framework to avoid possible cross-browser compatibility issues like you might be experiencing right now.

4xx Response from server doesn't include JSON data

I've got a .NET MVC site that uses JSON to perform AJAX form Posts. If a validation error occurs (ie user misses a required field etc), then the server returns the validation errors in a JSON object and sets the HTTP status code of the response to something in the 400 range. On our local machines, this works absolutely fine.
However on our CI environment, it has suddenly stopped working, without any code changes. The response comes back from the server with the correct HTTP code, but the content is not the JSON our controller returns, but the standard .NET error page HTML, ie just the 11-byte 'Bad Request' response if the status code is 400.
The error code is correct for each validation error, so we are hitting the right controller/action, and the validation is working correctly, but for some reason our JSON is getting snipped out. Any ideas why this might be happening?
You are getting 400 code because your request syntax in not correct. Check if you have actually encode json data correctly or not.

Why do I get 404 with a valid url using Net::HTTP.post_form?

I use the following code to pass data into a website:
require "net/http"
params = {"message"=>"some message", "to"=>"someone"}
Net::HTTP.post_form(URI.parse("http://example.com/m/send"),params)
When I inspect the web page, the form action is http://example.com/m/send and I can post the data using the site itself without any problem.
I keep getting HTTP 404 and my data is not passed to the database.
When I request the page with GET method, then I get HTTP 405, which is an unauthorized request error. This guarantees that the page exists.
Since the url is valid, what would prevent the data being posted? And how can I fix that?
I could not solve the question using Net/HTTP library solely; however, Mechanize gem as Tin Man suggested in the comments solves the problem and successfully posts the data into the server.
It is also more flexible and easier in terms of following redirection. Hence, if anyone runs into this problem like I did, I recommend them using the Mechanize gem.

Wicket.Ajax.Call.failure: Error while parsing response: Object required

I just spent several hours of my life debugging this problem. I'm documenting it here for others.
Question:
I'm getting the following error when I try to click on an AjaxLink in Internet Explorer:
Wicket: ERROR: Wicket.Ajax.Call.failure: Error while parsing response: Object required
It works fine in all other browsers; just IE is busted.
Check to make sure that your HTML is 100% syntactically correct. Ajax responses are returned to the browser inside a CDATA section, and if the payload is not well-formed, IE will sometimes choke.
In my case I neglected to close a <link> tag in the <head> section. Simply closing that link tag made all the difference.
Aside: if you ever come across a tough-to-solve problem in Wicket, it's a good idea to create a quickstart project that reproduces your issue. It can be a lot of work to boil things down, but in doing so you often find the source of the problem.
I want to note one more potential reason for the issue with Wicket's AJAX in IE. It might help someone, who encounters similar problem.
In my case I had the following error message in IE:
Wicket: ERROR: Wicket.Ajax.Call.failure: Error while parsing response: could not find root <ajax-response> element
The reason was an incorrect Content-Type of AJAX response. I used AbstractTransformerBehavior and there was a bug in Wicket 1.4.x so this behavior was rewriting response Content-Type with text/html. IE does not parse such response as XML.

Resources