var req = $.ajax({
type: 'GET',
cache: false,
url: 'loc.aspx?count=' + str,
dataType: 'json',
contentType: "application/json; charset=utf-8",
async: false,
data:'{}',
success: function (data) {
alert(data.responseText);
}
});
req.fail(function (data) {
TINY.box.show({ html: 'Server Error. Try again: ' + data.responseText, animate: false, close: false, boxid: 'error', top: 100 });
});
the above code used to work right in jsp, now i am trying to use in asp.net c#, any way I am getting correct data in error block which i want it in success block. Even data.d is not helping,
If i write something like alert(data) i am getting the complete html, I need just the response text, When i use like this data.responseText, I am getting undefined. Someone help pls.
Thanks
Code below should work fine. I’ve added some comments where you are doing a mistake
In
.failfunction you havedata.responseTextbecause in this case XHR object is passed as a first parameter (See this). At the same time, first parameter ofsuccesscallback is a clean data received with request. If without details, you can think thatdata == xhr.responseTextin success callback. See success property description in this sectionupd
As appeared, problem is not only with JS. I guess you have a simple
.aspxpage which is called with ajax. There are two solutions:1) Use webservice instead. It will be better because it does not go through complete page load cycle and should be executed faster. And it will not produce unused HTML
2) aspx page will output some HTML by default. Before you return a response for ajax request, you should clear response (so HTML which is already generated will not be sent to a server), write your response and immediately end it:
This way you should receive only
helloin success.