0

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.

9
  • And what error are you getting? Posting the actual error would be more helpful than anything else you've posted ? Commented Aug 1, 2014 at 6:07
  • to parse json, you can use $.parseJSON() method Commented Aug 1, 2014 at 6:08
  • It should be noted that there is no failure property, so you can remove that, it will never be fired. Commented Aug 1, 2014 at 6:10
  • Do you get your json from server side on client side or you just dump on server side? Commented Aug 1, 2014 at 6:14
  • Please look @ your network tab in your browsers dev tools, to tell us: What causes the error and which error was thrown. We need more information (like Http-Status-Code & parameter encoding), since the only possible solution is that your error occurs on the server side. Commented Aug 1, 2014 at 6:16

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.