Jquery 1.3.2 Ajax how to get response
Hi below is the code, i am calling GetAjaxComments function which sends aja开发者_运维技巧x request and successfull completed but i am not getting any values in response (eg:server side function returned DbComment ="comment already exist"). Please help.
function GetAjaxComments(sEmployeeCompensationID, iInfoType) {
$(document).ready(function() {
var json = "{'EMPLOYEECOMPENSATION_ID':'" + sEmployeeCompensationID
+ "','iInfoType':'" + iInfoType
+ "'}";
var ajaxPage = "AjaxGridRowSave.aspx?SaveRow=2"; //this page is where data is to be processed
var options = {
type: "POST",
url: ajaxPage,
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
complete: function(data) {
},
error: function(msg) {
if (msg.statusText != "OK") {
OpenInfoPopup("Alert", "Error occured while retriving data.");
}
}
};
});
var returnText = data;
if (returnText != '') {
var dbValues = new Array();
dbValues = eval('(' + returnText + ')');
if ((dbValues1.DbComment) && (dbValues1.DbComment != null) && (dbValues1.DbComment != '')) {
switch (dbValues1.iInfoType) {
case 1: OpenInfoPopup('Eligibility Override Reason', dbValues1.DbComment); break;
case 2: OpenInfoPopup('Eligibility Override Rejection Reason', dbValues1.DbComment); break;
case 3: OpenInfoPopup('Rejection Reason', dbValues1.DbComment); break;
case 4: OpenInfoPopup('Guideline Override Reason', dbValues1.DbComment); break;
case 5: OpenInfoPopup('Guideline Override rejection Reason', dbValues1.DbComment); break;
case 6: OpenInfoPopup('Pay Range Override Reason', dbValues1.DbComment); break;
case 7: OpenInfoPopup('Pay Range Override Rejection Reason', dbValues1.DbComment); break;
}
}
}
It seems that jQuery doesn't set the content-type in the HttpRequestheader properly.
Add the following:
beforeSend: function(xhr){
xhr.setRequestHeader('Content-Type', 'application/json');
},
To the Ajax call.
Your code makes no sense what-so-ever.
Your code does not initate an AJAX request. You need to call one of jQuery's AJAX functions to start an AJAX request using jQuery. Containing $(document).ready()
like this makes no sense and will not be doing what you're expecting it to do.
$(document).ready()
attaches a handler which is executed on page load. As the page load will almost certainly have happened when this function is executed, the provided function will be executed immediately, and will allocate a few local variables (all of which will be unavailable outside the scope of the function.
What you most probably want is:
function GetAjaxComments(sEmployeeCompensationID, iInfoType) {
var json = "{'EMPLOYEECOMPENSATION_ID':'" + sEmployeeCompensationID + "','iInfoType':'" + iInfoType + "'}";
var ajaxPage = "AjaxGridRowSave.aspx?SaveRow=2"; //this page is where data is to be processed;
var returnText = '';
jQuery.ajax({
type: "POST",
url: ajaxPage,
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(response) {
returnText = response;
},
error: function(msg) {
if (msg.statusText != "OK") {
OpenInfoPopup("Alert", "Error occured while retriving data.");
}
}
};
});
if (returnText != '') {
var dbValues = new Array();
dbValues = eval('(' + returnText + ')');
if ((dbValues1.DbComment) && (dbValues1.DbComment != null) && (dbValues1.DbComment != '')) {
switch (dbValues1.iInfoType) {
case 1:
OpenInfoPopup('Eligibility Override Reason', dbValues1.DbComment);
break;
case 2:
OpenInfoPopup('Eligibility Override Rejection Reason', dbValues1.DbComment);
break;
case 3:
OpenInfoPopup('Rejection Reason', dbValues1.DbComment);
break;
case 4:
OpenInfoPopup('Guideline Override Reason', dbValues1.DbComment);
break;
case 5:
OpenInfoPopup('Guideline Override rejection Reason', dbValues1.DbComment);
break;
case 6:
OpenInfoPopup('Pay Range Override Reason', dbValues1.DbComment);
break;
case 7:
OpenInfoPopup('Pay Range Override Rejection Reason', dbValues1.DbComment);
break;
}
}
}
}
You should read up on jQuery's ajax function
Using synchronous AJAX requests is extremely bad practise; the browser will be locked and unusable for the duration of the request. You should seriously consider handling the response using a callback. I've outlined how this works in another StackOverflow question, but in your example you can change your code to:
function GetAjaxComments(sEmployeeCompensationID, iInfoType) {
var json = "{'EMPLOYEECOMPENSATION_ID':'" + sEmployeeCompensationID + "','iInfoType':'" + iInfoType + "'}";
var ajaxPage = "AjaxGridRowSave.aspx?SaveRow=2"; //this page is where data is to be processed;
jQuery.ajax({
type: "POST",
url: ajaxPage,
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(returnText) {
if (returnText != '') {
var dbValues = new Array();
dbValues = eval('(' + returnText + ')');
if ((dbValues1.DbComment) && (dbValues1.DbComment != null) && (dbValues1.DbComment != '')) {
switch (dbValues1.iInfoType) {
case 1:
OpenInfoPopup('Eligibility Override Reason', dbValues1.DbComment);
break;
case 2:
OpenInfoPopup('Eligibility Override Rejection Reason', dbValues1.DbComment);
break;
case 3:
OpenInfoPopup('Rejection Reason', dbValues1.DbComment);
break;
case 4:
OpenInfoPopup('Guideline Override Reason', dbValues1.DbComment);
break;
case 5:
OpenInfoPopup('Guideline Override rejection Reason', dbValues1.DbComment);
break;
case 6:
OpenInfoPopup('Pay Range Override Reason', dbValues1.DbComment);
break;
case 7:
OpenInfoPopup('Pay Range Override Rejection Reason', dbValues1.DbComment);
break;
}
}
}
},
error: function(msg) {
if (msg.statusText != "OK") {
OpenInfoPopup("Alert", "Error occured while retriving data.");
}
}
});
};
精彩评论