Found several other questions with this topic, but they were syntax errors of people closing their AJAX call too early which I don’t appear to have done…
Here’s my AJAX call in my jqGrid loadComplete:
loadComplete : function() {
$.getJSON("getCurrentProject.html", function(results){
alert(results);
$(this).jqGrid('setCaption',"Project: " + results);
});
},
As you can see, I’m simply calling a URL to get back a currentProject String and set the caption on the jqGrid accordingly.
Trouble is, the success function is never being invoked! I looked in Firebug and I can see the server responding with the single String value I expect (“default” in this case).
What am I missing here? Should I just be using get instead of getJSON?
Only use
.getJSON()if you’re expecting the response to actually be JSON. In your case you’re expecting a string, so try:You may not need the last parameter to
.get()to set the expected type to “text”, but then again maybe you will – if you don’t supply it jQuery will make an “intelligent guess” at the response type and it may guess incorrectly.Note that within your ajax callback
thiswill not be the same as in theloadCompletefunction so you need to keep a reference to that in a variable outside the ajax callback, as shown.