tags:

views:

16

answers:

2

Hi,

It is my code:

$.ajax({

            type: "POST",
            url: "Default.aspx",
            data: "category=2&city=Boston",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData :false,
            error: function (json) {
                alert(json.statusText);
            },
            success: function (json) {
                alert(json.statusText);

            }
        });

How can I get my parameters(category,city) on the server? Request's parameters is always null.

Thanks, kukuwka

A: 

You don't write anything about the server, but you use both contentType and dataType contains JSON. So probably you want to send JSON data to the server and receive also JSON data back. The data from the string "category=2&city=Boston" are not in JSON format.

You should replace the string "category=2&city=Boston" to 'category=2&city="Boston"'. I recommend you to read my old answer http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086 and use JSON.stringify function (a part of json2.js which can be downloaded from http://www.json.org/js.html)

So your code should be better modified to the following

var myCategory = 2;
var myCity = "Boston";
$.ajax({
    type: "POST",
    url: "Default.aspx",  // probably it should by a ASMX amd not ASPX page?
    data: { category: myCategory, city: JSON.stringify(myCity) },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    error: function (json) {
        alert(json.statusText);
    },
    success: function (json) {
        // you should probably modify next line because json can be an object
        alert(json.statusText);
    }
});
Oleg
A: 

Something like this works for me calling a servlet in JAVA, not sure about .NET but it's worth a try.

$.ajax({
     type: "GET",
     url: "/Default.aspx?category=2&city=Boston"
     //more config stuff here
});
Randall Kwiatkowski