0

I am trying to implement the same code that was mentioned in this question

Currently I have the following code:

var pagePath = window.location.pathname;

var paramList = '';
if (paramArray.length > 0) {
 for (var i = 0; i < paramArray.length; i ++) {
  if (paramList.length > 0) paramList += ',';
  paramList += "{'id':'" + paramArray[i].id + "', 
                       'collapsed':'" + paramArray[i].collapsed + "', 
                       'order':'" + paramArray[i].order + "', 
                       'column':'" + paramArray[i].column + "'}";
 }
}
paramList = '[' + paramList + ']';

$.ajaxSetup({ cache: false });
//Call the page method  
$.ajax({
 type: "POST",
 url: pagePath + "/" + fn,
 contentType: "application/json; charset=utf-8",
 data: "{'items': **'**" + $.toJSON(paramList) + "**'**}",

dataType: "json", success: successFn, error: errorFn });

I am trying to pass this data to the WebMethod

[WebMethod]
public static String SaveData(Dictionary<String, Object>[] items)

The problem is that I keep receiving the error "500 Internal Server Error". I'm pretty sure that the data type is causing the problem but just can't figure it out. Any ideas?

8
  • Did you have a look in e.g. firebug what your post data looks like? The Asterixes within the JSON string look a little bit strange to me. Commented Feb 16, 2010 at 11:06
  • I think you don't need to serialize the paramList, you already did it. now you are just sending a string. see my answer. I think you will have success if the parameters match the pagemethod Commented Feb 16, 2010 at 12:17
  • yeah used firebug and my post data looks like this: "[ {'id':'1', 'collapsed':'0', 'order':'0', 'column':'column2'},{'id':'2', 'collapsed':'1', 'order':'1', 'column':'column2'},{'id':'3', 'collapsed':'0', 'order':'0', 'column':'column3'} ]" Commented Feb 16, 2010 at 12:20
  • you want your paramList string to look similar to this var postData = '{input: { Name: "Foo", Age: 21 }}'; Notice param names not quoted, integer not quoted, and if there was a bool, it would not be quoted. See listing 6 and 7 in the article I linked under my answer. You are almost there. Commented Feb 16, 2010 at 12:20
  • now what does the pagemethod signature look like? Commented Feb 16, 2010 at 12:21

1 Answer 1

0

Is your service decorated with [ScriptService] attribute?

and what's up with the asterisks? is this some new macro i didn't get the memo on?

"{'items': **'**" + $.toJSON(paramList) + "**'**}"

try

"{'items': " + $.toJSON(paramList) + "}"

As i see it, you are already creating json. no need for the toJSON

try replacing the single quotes with double quotes. e.g.

'{"items": ' + paramList + '}'

your post data should look like this

'{
    "items": [{
        "id": "1",
        "collapsed": "0",
        "order": "0",
        "column": "column2"
    },
    {
        "id": "2",
        "collapsed": "1",
        "order": "1",
        "column": "column2"
    },
    {
        "id": "3",
        "collapsed": "0",
        "order": "0",
        "column": "column3"
    }]
}'

inline

'{ "items": [{ "id": "1", "collapsed": "0", "order": "0", "column": "column2" }, { "id": "2", "collapsed": "1", "order": "1", "column": "column2" }, { "id": "3", "collapsed": "0", "order": "0", "column": "column3" }]}'



var paramList = "";
if (paramArray.length > 0) {
 for (var i = 0; i < paramArray.length; i ++) {
  if (paramList.length > 0) paramList += ",";
  paramList += '{"id":"' + paramArray[i].id + '", 
                       "collapsed":"' + paramArray[i].collapsed + '", 
                       "order":"' + paramArray[i].order + '", 
                       "column":"' + paramArray[i].column + '"}';
 }
}
paramList = "[" + paramList + "]";

$.ajaxSetup({ cache: false });
//Call the page method  
$.ajax({
 type: 'POST',
 url: pagePath + '/' + fn,
 contentType: 'application/json; charset=utf-8',
 data: '{"items":' + paramList + '}',  // the rest of your function is missing
Sign up to request clarification or add additional context in comments.

4 Comments

No, I am not using [ScriptService] as I am using a Page Method and not a Service. Got the asterisks from the link I posted... changed it to your suggestion but am still receiving error 500 :(
what is the method signature on the pagemethod?
as background on how parameters are dealt with, you can see codeproject.com/Articles/38999/…
did all the changes you suggested and still getting the same error

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.