handle json response in jquery ajax

Shaggy

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}
Nunners

You are getting {"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 :

[WebMethod]
public static string 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) {
            return dtOutput.Rows[0][0].ToString() + "|" + dtOutput.Rows[0][1].ToString();
        }
    } catch (Exception xObj) {
        return "ERROR: " + xObj.Message;
    }
}

And then the returned object will be {"d":"JAMES|BOND"}, which can be accessed via response.d in your javascript.

$.ajax({
    url: 'ajaxExecute.aspx/GETCUST',
    data: strRequest,
    dataType: 'JSON', // Changed dataType to be JSON so the response is automatically parsed.
    contentType: "application/json",
    cache: false,
    context: document.body,
    type: 'POST',
    error: function (xhr) {
        alert(xhr.responseText);
    },
    success: function (response) {
        alert(response.d); // Should correctly alert JAMES|BOND
    }
});

Please note that in the Javascript I have changed the dataType of the Ajax response to be JSON so that the response is parsed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using jQuery to build table rows from AJAX response(json)

From Dev

How to call ajax request with json response using jquery?

From Dev

Jquery AJAX POST to IE 7, handle response

From Dev

how to print json array in jquery with ajax response

From Dev

Use Jquery ajax json response?

From Dev

How to populate dropdownlist with JSON data as ajax response in jQuery

From Dev

jQuery looping through ajax json response

From Dev

Mapping JSON AJax response to JQuery auto complete

From Dev

Jquery ajax populate dropdown with json response data

From Dev

jQuery: return string response in jquery ajax json call

From Dev

Is it possible to submit a POST, not AJAX, and get a JSON response in jQuery?

From Dev

How to handle Ajax JSON response?

From Dev

handle JSON response with SwiftyJSON

From Dev

Getting Java Object as JSON response using Jquery AJAX

From Dev

How to handle non-json response while using jquery $.post?

From Dev

JQuery Ajax Json Response Does not Work

From Dev

How to handle 204 response from ajax request in jquery

From Dev

jQuery AJAX can't work with JSON response

From Dev

jQuery Ajax Json response - check if is null

From Dev

Jquery AJAX POST to IE 7, handle response

From Dev

Jquery ajax populate dropdown with json response data

From Dev

Cannot Bind JSON response to textbox using jquery ajax (MVC 4)

From Dev

jQuery: return string response in jquery ajax json call

From Dev

Jquery/Ajax - Parse JSON Response

From Dev

JSON returning null on response with jQuery $.ajax

From Dev

Jquery ajax call response JSON format

From Dev

jQuery Ajax Json response - check if is null

From Dev

jQuery AJAX Request - Handle special characters in response from PHP page

From Dev

JQuery + ajax + json fail to show response

Related Related

  1. 1

    Using jQuery to build table rows from AJAX response(json)

  2. 2

    How to call ajax request with json response using jquery?

  3. 3

    Jquery AJAX POST to IE 7, handle response

  4. 4

    how to print json array in jquery with ajax response

  5. 5

    Use Jquery ajax json response?

  6. 6

    How to populate dropdownlist with JSON data as ajax response in jQuery

  7. 7

    jQuery looping through ajax json response

  8. 8

    Mapping JSON AJax response to JQuery auto complete

  9. 9

    Jquery ajax populate dropdown with json response data

  10. 10

    jQuery: return string response in jquery ajax json call

  11. 11

    Is it possible to submit a POST, not AJAX, and get a JSON response in jQuery?

  12. 12

    How to handle Ajax JSON response?

  13. 13

    handle JSON response with SwiftyJSON

  14. 14

    Getting Java Object as JSON response using Jquery AJAX

  15. 15

    How to handle non-json response while using jquery $.post?

  16. 16

    JQuery Ajax Json Response Does not Work

  17. 17

    How to handle 204 response from ajax request in jquery

  18. 18

    jQuery AJAX can't work with JSON response

  19. 19

    jQuery Ajax Json response - check if is null

  20. 20

    Jquery AJAX POST to IE 7, handle response

  21. 21

    Jquery ajax populate dropdown with json response data

  22. 22

    Cannot Bind JSON response to textbox using jquery ajax (MVC 4)

  23. 23

    jQuery: return string response in jquery ajax json call

  24. 24

    Jquery/Ajax - Parse JSON Response

  25. 25

    JSON returning null on response with jQuery $.ajax

  26. 26

    Jquery ajax call response JSON format

  27. 27

    jQuery Ajax Json response - check if is null

  28. 28

    jQuery AJAX Request - Handle special characters in response from PHP page

  29. 29

    JQuery + ajax + json fail to show response

HotTag

Archive