kukuwka
kukuwka

Reputation: 29

How to get jquery/ajax parameters on the server

This 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=2&city=Boston) to the server? Request parameters is always null.

Upvotes: 1

Views: 14844

Answers (2)

Randall Kwiatkowski
Randall Kwiatkowski

Reputation: 895

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
});

Upvotes: 0

Oleg
Oleg

Reputation: 221997

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 How do I build a JSON object to send to an AJAX WebService? 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);
    }
});

Upvotes: 4

Related Questions