Dot NET
Dot NET

Reputation: 4897

Unable to pass data to method

I am trying to use AJAX with ASP.NET for the first time - by trying to pass the following data to a WebMethod in my corresponding aspx page:

$.ajax({
    type: "POST",
    url: "myurl.aspx/SaveScreen",
    data: "{'data': " + JSON.stringify(arrayRows.data) + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert(msg.d);
    }
});

Here is my WebMethod in the aspx page. I have tried passing a simple data object which just contained one key and value, which worked successfully. When attempting to pass this object, I get an error stating that there is an internal server error with code 500.

What could I be doing wrong?

Upvotes: 1

Views: 238

Answers (3)

Robert Fricke
Robert Fricke

Reputation: 3643

You could wrap the data string in quotations:

$.ajax({
    type: "POST",
    url: "SpecifyPositioning.aspx/SaveScreen",
    data: "{'data': '" + JSON.stringify(arrayRows.data) + "'}", //Added ' around JSON.stringify to pass string instead of object
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert(msg.d);
    }
});

This however would be a cleaner solution:

data: {"data": JSON.stringify(arrayRows.data)},

Upvotes: 0

Roman Izosimov
Roman Izosimov

Reputation: 210

Try this: data: {"data": JSON.stringify(arrayRows.data)}

Upvotes: 2

Plue
Plue

Reputation: 1790

Try this :

data: "{data: '" + JSON.stringify(arrayRows.data) + "'}"

Upvotes: 0

Related Questions