3

I am a newbie at javascript and jquery and I would like some help if possible. I searched and tried to make it work, but I think I am missing something simple.

I have the following method in my cs file (CeduleGlobale.aspx.cs)

[WebMethod]
     public static void SetSession(string data)
     {
         HttpContext.Current.Session["salesorderno"] = data;
     }

I also have a some javascript in my ascx file

<script type="text/javascript">
    function SetSession() {

        var request;

        var values = 'fred';
        request = $.ajax({
            type: "POST",
            url: "CeduleGlobale.aspx/SetSession",
            data: values,
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });

        request.done(function () {
            alert("Finally it worked!");
        });

        request.fail(function () {
            alert("Sadly it didn't worked!");
        });


    }
</script>

The function in the script is called by

<dx:ASPxCheckBox ID="cbxHold" runat="server" AutoPostBack="true" Text="OnHold" ClientSideEvents-CheckedChanged="SetSession">
</dx:ASPxCheckBox>

And the result i keep getting is "Sadly, it didn't work!".

I know the problem is not with anything relative to the path of the url, because it worked when i passed NULL as data and had the method with no parameters.

The parameters and data is what i tripping me I believe.

1
  • Your options say you are sending JSON data but your values are not in valid JSON form. Commented Jul 12, 2013 at 13:22

5 Answers 5

2

You should pass serialized JSON into the method:

var values = JSON.stringify({data:'fred'});
request = $.ajax({
    type: "POST",
    url: "CeduleGlobale.aspx/SetSession",
    data: values,
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});
Sign up to request clarification or add additional context in comments.

2 Comments

or values = "key1=value1&key2=value2"
First off all, thanks a lot for all the answers, this community is awesome and very helpful. I did what you recommended and I am now getting a Microsoft JScript runtime error: 'JSON' is undefined error is the Visual Studio debugger.
2

You are specifying that you are sending JSON, but you don't serialize the value to JSON, so try changing the request to this:

request = $.ajax({
        type: "POST",
        url: "CeduleGlobale.aspx/SetSession",
        data: JSON.stringify({data: values}), // 'stringify' the values to JSON
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });

Comments

2

The data sent through post should be sent in a {key:value} format values={name:'fred'}

Comments

1

'fred' is not json nor object

use object notation :

{"myattr":"fred"} //you can also do {myattr:"fred"}

and then use JSON.stringify which transform it into STRING representation of json object.

Comments

1

The data should be passed into [key:value] pair.

Comments

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.