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);
});
Related
How does jsf.ajax.addOnError actually catch errors?
I didn't find any information about it's mechanism. I only found that it is a error listener.
Wherever you've found the statement that jsf.ajax.addOnError is an error-listener, that source is wrong. The addOnError function adds an error listener (ie, a function you define yourself and that gets called whenever the JSF framework encounters an error situation).
This is straight out of the JSF-2.2 spec, 13.3.6.2:
The jsf.ajax.addOnError function accepts a JavaScript function argument that will be
notified when errors occur during any Ajax request/response cycle. [P1-start-event] The implementation must ensure the JavaScript function that is registered must be called in accordance with the errors outlined in Section TABLE 14-5 “Errors”.[P1-end]
Thus, the "errors" table defines under which conditions your function will get called. Here they are:
httpError: request status==null or request.status==undefined or request.status<200 or
request.status >=300
serverError: The Ajax response contains an “error” element.
malformedXML: The Ajax response does not follow the proper format.
emptyResponse: There was no Ajax response from the server.
JSF implementations basically fire a Ajax request and define internal handlers that get called by the browser when a response arrives. Then, they are required to inspect the response and if one the above conditions are met, they look up if you have registered any functions to be called and execute them if need be (they do a lot more, but that's the part in question here).
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.
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 :(
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.
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.