I want to retrieve data from database as a single row of string separated with '|' for which i am using json/ajax/WebMethod
JS
var request = {
RefNo: $('#txtRefNo').val()
};
var strRequest = JSON.stringify(request);
$('#divDialog').html('<div>Retrieving Information...</div>').dialog({ title: 'Please Wait...', modal: true, resizable: false, draggable: false });
$.ajax({
url: 'ajaxExecute.aspx/GETCUST',
data: strRequest,
dataType: "text",
contentType: "application/json",
cache: false,
context: document.body,
type: 'POST',
error: function (xhr) {
alert(xhr.responseText);
},
success: function (response) {
alert(response);
}
});
C#
[WebMethod]
public static void GETCUST(string RefNo)
{
try
{
DataTable dtOutput = new DataTable();
dtOutput = Generix.getData("dbo.customers", "[first_name],[middle_name]", "reference_no='" + RefNo + "'", "", "", 1);
if (dtOutput.Rows.Count > 0)
{
HttpContext.Current.Response.Write(dtOutput.Rows[0][0].ToString() + "|" + dtOutput.Rows[0][1].ToString());
}
}
catch (Exception xObj)
{
HttpContext.Current.Response.Write("ERROR: " + xObj.Message);
}
}
I am getting output with {"d":null} in it. How to remove it from response ? or am i doing something wrong in code
Output:
JAMES|BOND{"d":null}
{"d":null}because your WebMethod has no return value, you are just writing to the Response object. You should return a string from your method. And then the returned object will be{"d":"JAMES|BOND"}, which can be accessed viaresponse.din your javascript.