jquery ajax error json response - ajax

i have jquery global error event set, like following:
$("#message_alert").ajaxError(function(event, XMLHttpRequest, settings, thrownError){
ajax_error(XMLHttpRequest);
});
and ajax_error method gets the XMLHttpRequest parameter totally fine init. now the request which XMLHttpRequest gets, it also have json data from the backend in XMLHttpRequest.responseText
now i want to know, how can i parse this json data, i tried doing
eval("var request = "+XMLHttpRequest.responseText);
which for some reason was working fine, but dose not work anymore and i know for sure that data is getting back to ajax response. maybe something im doing wrong.. well firebug shows following error from it, i dont know what im doing wrong
Error:
missing ; before statement
http://basit.io.im/javascript/global.js
Line 127
btw this is the same eval line number. any ideas?

Have you tried -
eval("var request = XMLHttpRequest.responseText");
That should fix the error you are getting.

Related

500 error even request render HTML code

Using ajax to get HTML content from GSP template .
$.get(url,{word:$('#search').val()},fnback)
The browser Console raises 500 error .
However , we get the expected response , but in browser not in callback .
Known that this kind of error appears only in production environment .
This question is related to this ticket
The error is caused either by Grails or by your application, you will need to determine why; it certainly seems to be happening relatively late in the pipeline since you are getting the correct HTML back (I assume you aren't explicitly rendering a 500 status code in your code by accident).
As for the response you are getting back, it is ignored due to the 500 status. The $.get function accepts a callback which is only invoked on successful requests. If you put debug lines into your fnback function you will see it is never called. If you were to replace the $.get with an equivalent $.ajax call and provide an error callback, that function would get the HTML you are seeing returned in the browser's dev tools.
Based on #Gregor Petrin answer :
$.get(myurl,{word:word},function(d){
$('div#resp').html(d)
})
has been replaced by :
$.ajax({url:myurl,data:{word:word}}).always(function(d,status){
if(status !=='success'){
d=d.responseText;
}
$('div#resp').html(d);
});

ajax call to cross domain using jsonp

Hello I am struggling to get this work.
var url = 'http://xxxx/getCustomerCardInfo?requestor_email=honey#gmail.com&callback=?';
$.getJSON(url, function(data){
alert(data);
});
The ajax call is successful. But this url gives the JSON in response i.e
{"targetRequestUri":"/getCustomerCardInfo","javax.servlet.request.key_size":256,"outputMap":{"emailId":"honey#gmail.com","orderList":[{"orderId":"ST210340","orderDate":"2013-04-24 07:12:54.187","orderStatus":"ORDER_COMPLETED","totalMoney":1}],"partyId":"10810","customerName":"honey goyal","telephoneNumber ":"9023605155"},"_FORWARDED_FROM_SERVLET_":true,"javax.servlet.request.cipher_suite":"DHE-RSA-CAMELLIA256-SHA","thisRequestUri":"json"}
But my Firefox error console gives the error in JSON:-
SyntaxError: invalid label
on second character of beginning of JSON, i mean on " in
{"targetRequestUr
And I does not get any alert. Any idea what i was doing wrong.
I think this need only JSONP response and padding is missing in above JSON.
Got the solution. I need to append padding before the JSON from web Server.
But this is not perfect solution according to me because it force me to change third party code. Still waiting for the perfect one so it can work by only changes to client side scripting. like
functionName({"firstName": "John","lastName": "Smith","age": 25});

Why is my json request erroring instead of succeeding?

Here is my debugging method that goes to the error block instead of the success block.
function removerelationship(reference_related_id_var) {
if ($('##relationships').attr('id') != undefined) {
$.ajaxSetup({cache:false});
$.ajax({
url: 'index.cfm?action=reference.confirmjson',
dataType: 'json',
data: {reference_id:reference_id_var, reference_related_id:reference_related_id_var},
success: function(){alert("I PASSED");},
error: function(){alert("I FAILED");}
});
But this is my response from calling reference.confirmjson:
{"MESSAGE":"Are You Sure You Want To Remove The Relationship Between References 744094 and 1200?","CONFIRMED":true}
Is there some reason this would still take me to the error block?
Thanks.
Make sure you have debug output turned off for the AJAX request . I explain it a bit better at http://orangexception.com/post/7308110027/remove-debug-output-from-ajax-requests-in-coldfusion
The error case would be called if any status other than a 200 is being returned. Take a peek at the response in Firebug or a similar tool. If CF is also throwing an error further down the request, it would return a 500. This can help you determine if you need to check the CF application log for an error.
Edit: Also, check the raw response. Firebug does an awesome job at dropping the trailing CF error and just showing the properly formatted JSON, which could be confusing if an error was thrown.

Callbacks on Ajax.BeginForm don't work right

why do I always have so much trouble...? given that I didn't solve the problem in my other article, I decided to just code the javascript right into the values... so I have:
OnSuccess="alert('ok')",
OnFailure="alert('failed')",
so my problem is the submission works fine; a record gets inserted into the database and I get a callback... but I get the wrong callback! I get a failure even though the record got inserted. heeeeelp!
You should be able to read data from the response to figure out why it's considered a failure:
OnFailure="handleError",
...
function handleError(ajaxContext) {
var response = ajaxContext.get_response();
var statusCode = response.get_statusCode();
alert("Sorry, the request failed with status code " + statusCode);
}
Alternatively, use Fiddler and look at the response. Make sure the status code, content type and content are all as expected.
ok, I figured out a few things:
OnFailure="handleError" is the correct way to do this (see the other article I mentioned for resolution)
ajaxContext didn't have a get_response() method because I was actually hooking up the function to the OnComplete event instead (my bad)! once hooked up to the OnSuccess, I get my controller's method Json return value natively
I was getting the OnSuccess handler called when the database entry was failing. this is because my controller method was try{} catch{}ing and therefore never failed! me being dopey :(

JQuery ajax calls not working in Firefox browser

I am trying to test Jquery ajax calls in Firefox but it it not working. I mean my server is not receiving any requests. But when I test in IE8 it works fine. Here is my ajax call:
$("#getWeatherReport").click(function(){
$cityName = "New York";
$.ajax({
type: "POST",
dataType:"xml",
url: "http://localhost:8080/Test/WeatherServlet",
data: "cityName="+$cityName,
success: function(data) {
alert($("report", data).text());
},
error: function(xhr, textStatus, errorThrown) {
alert('ERROR['+xhr.statusText+']');
}
});
});
It is not even calling error function. And from my server code(java) I am setting content type as "text/xml".
Any suggestions?
Your string is not correctly serialized, I'm not sure if that's the issue, but it may be and it's definitely a potential one for later, try this for an immediate test:
var $cityName = "New+York";
As a more permanent solution, pass data as an object, like this:
data: {cityName: $cityName},
Have you installed Firebug?
Your best bet would be to install Firebug, which comes with a console that'll notify you of any javascript errors. You can also use it (via the "Net" tab) to monitor all requests made by your page.
From what I can see, your code looks OK (other than the possible issue pointed out by #Nick Craver)
Also, why the '$' on your cityName variable? The '$' prefix in Javascript is meant to be reserved for machine-generated code (so that it has no chance of conflicting with user code).
try installing firebug plugin in ff :: https://addons.mozilla.org/en-US/firefox/addon/1843/
Then check the :::: Net Tab >> All selected
Refresh the page and see is your ajax call actually getting called. If yes is there any syntax error in the call or any variable null error. If all is fine then you can think of further issues
Usually, when I end up with a parseerror that means that the return header type is wrong or that somehow the server sent extra data with the response. For instance, if I'm looking to get JSON back and I get the JSON and some HTML from x-debug.
Also, The OPTIONS request is for cross-domain requests which is what #Nick was alluding to.
A helpful link to get you started.

Resources