0

I have searched a lot and not been able to find a working solution to why my post request is not sending it's data to the server. I can send the request without data and I get my results from the server, but I just cannot send my data to the server. I have narrowed it down to the "data" attribute and assume I am just doing something wrong. Thank you.

Client

var scriptURL = "default/scripts/serverside/Scripts.aspx";
$.ajax({
    type: "POST",
    url: baseURL + scriptURL + "/SaveItem",
    data: "{}",                                           //works (to return a result)
    //data: "{sendData: '" + dataPackage + "'}",            //does not work
    //data: dataPackage,                                    //does not work
    //data: { sendData: dataPackage },                      //does not work
    //data: { "sendData": dataPackage },                    //does not work
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        de("server result:" +result);
    }
});

Server

[WebMethod]
public static string SaveItem(string sendData)
{
    string result = "received: " + sendData;
    return result;
}

Please help, I just cant seem to get it working and know it has got to be a syntax issue...

Similar problems I have found (but no working answers):

7
  • Can you open up your browser's Javascript debugger and bring up a page with this edit: $.post('baseURL + scriptURL + "/SaveItem"', { sendData: 'foobar'} ); Jul 22, 2013 at 15:59
  • You should pass the data as a string. Jquery will serialize the string itself. See also: stackoverflow.com/questions/1061884/…
    – Remko
    Jul 22, 2013 at 16:03
  • @chrisHardie, ran it with "dataPackage" in quotes and checked the debugger, it simply says I am sending sendData=dataPackage as one large string...
    – yoyo
    Jul 22, 2013 at 17:07
  • @RMK, I just tried passing the whole things as a string and it didnt work...
    – yoyo
    Jul 22, 2013 at 17:09
  • Please don't add [solved] to your title or a summary of the answers to the question - the answers themselves serve the latter purpose and the checkmark serves the former. If you want add more detail past what the other answerers have given, please add your own answer with the details. Jul 22, 2013 at 17:43

2 Answers 2

1

Try this one:

$.ajax({
   type: "POST",
   url: baseURL + scriptURL + "/SaveItem",
   data: $.toJSON({ sendData: dataPackage }),                      
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function (result) {
       de("server result:" +result);
   }
});

The toJSON will convert your JS object into a proper JSON string. You could also use JSON.stringify

3
  • This is the closest I've gotten, but now the data is being passed to the server (so says firebug) but I cannot get a response with the result. I assume, you cannot simply declare the JSON incoming object as a string...
    – yoyo
    Jul 22, 2013 at 17:25
  • additionally, I had to add ', ScriptMethod(ResponseFormat = ResponseFormat.Json)]'
    – yoyo
    Jul 22, 2013 at 17:26
  • Cool! :) Glad you got it!
    – margabit
    Jul 22, 2013 at 17:41
0

Try this:

var scriptURL = "default/scripts/serverside/Scripts.aspx";
$.ajax({
    type: "POST",
    url: baseURL + scriptURL + "/SaveItem",
    data: {sendData: "string to send" }
    dataType: "json",
    success: function (result) {
        de("server result:" +result);
    }
});
1
  • I tried this and it didnt work. However, even if I send anything it should still respond with what the server received, right?
    – yoyo
    Jul 22, 2013 at 17:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.