user1502901
user1502901

Reputation: 123

Processing encoded json response from ajax request in jquery

I am trying to make a ajax request using jquery:

$.ajax({
    url: "/src/ax_query_places",
    dataType: "json",
    data: {
        query: value
    },
    success: function(response) {
        alert("Success");
    },
    failure: function() {
        alert('Could not get airports');
    },
    error: function() {
        alert('Error');
    }
});

I am encoding the json on server side. The json data received on response is :

{
    "places": [
        {
            "name": "New\x20Orleans,\x20US\x20\x28New\x20Lakefront\x20\x2D\x20NEW\x29",
            "code": "NEW"
        },
        {
            "name": "New\x20Bedford,\x20US\x20\x28All\x20Airports\x20\x2D\x20EWB\x29",
            "code": "EWB"
        }
    ]
}

But I am receiving error every time. Please suggest if there is a mistake in syntax. Or how should I parse the encoded response?

Also if I remove the encoding of json on the server side, everything works fine.

Upvotes: 0

Views: 114

Answers (1)

Subha
Subha

Reputation: 1051

May be you want to have a List of "places" and the "places" class will contain two properties "name" and "code".

In that case, first create a class named "places" with two properties as "name" and "code" (names should match)

From client side, send data: { query: JSON.stringify(value) }

catch a string value as parameter in server side. So, your method deceleration will be -

public YourReturnType YourMethodName(string query)

Inside that, use the following code -

var javaScriptSerializer = new JavaScriptSerializer();
var values = javaScriptSerializer.Deserialize<List<places>>(query);

You will get serialized JSON in values.

Hope this helps!

Upvotes: 1

Related Questions