3

I'm Using the Jquery Ajax Function to send data from an html page, & I'm reading it at the backend with C#.

When I used Get, the data were sent successfully, but when I switched to Post, the data value is null at the backend.

HTML:

<form onsubmit="AddData();>
<input type="submit"value="Submit" />
</form>

Javascript:

function AddData() {
            var RData = GetRData();
            var postData = { 'Function': "AddData", 'RData': JSON.stringify(RData) };
            $.ajax(
            {
                type: "POST",
                url: "/Services/GetRData.aspx",
                data: postData,
                contentType: "application/json; charset=utf-8",
                dataType: "jsonp",
                jsonp: 'jsoncallback',
                success: function (result) { 
                AddClubCallBackFunction(result) },
                error: function (msg) {
                    alert('Sorry, an error occured while adding your Data!');
                }
            });
        }

C#:

string FunctionName =Request["Function"]; //null
string RData = Request.Form["RData"]; //null

So, what am I doing wrong & how can I fix it ?

Update:

I just removed

contentType: "application/json; charset=utf-8",

and it worked.

Sana Joseph
  • 1,948
  • 7
  • 37
  • 58

1 Answers1

1

Assuming the above code is exactly what you are executing, there does appear to be a typo:

var Data = GetRData();
var postData = { 'Function': "AddData", 'RData': JSON.stringify(RData) };

Your variable is Data but your calling JSON.Stringify on RData which hasn't been declared (hence null property).

Update

Based on your last comment I would say it's a JSON formatting issue. This can generally happen when you attempt to manually construct JSON objects which you are partly doing in your solution. I would change your code to:

var rdata = GetRData();
var postData = { Function: 'AddData', RData: JSON.stringify(rdata) };
$.ajax({
    ...
    data: JSON.stringify(postData),
    ...
});
James
  • 80,725
  • 18
  • 167
  • 237
  • Yeah, you are right. I fixed it, but it's still not working. The "function is not correctly read too. – Sana Joseph Feb 11 '13 at 12:29
  • @SanaJoseph so given the change made, when you read the property on the server side it's still `null`? It's worth verifying that the data in your `RData` can actually be converted to JSON successfully e.g. output your `postData` to the console and verify it against a [JSON validator](http://jsonlint.com/) – James Feb 11 '13 at 12:31
  • Yes, nothing is returned, just null. – Sana Joseph Feb 11 '13 at 12:32
  • 1
    @SanaJoseph "*nothing is returned*" - are you talking about nothing is returned **from** the server or nothing is **sent** to the server? You need to be a bit more clear in what your saying as these are 2 completely different problems. – James Feb 11 '13 at 12:35
  • "Something" is sent . I even alerted the RData & became sure of what's sent in it. But at the aspx page, Request["RData"]'s value is null & Request["Function"] is null too. – Sana Joseph Feb 11 '13 at 12:43
  • 1
    @SanaJoseph that tells me that your JSON is invalid as the server is unable to parse it. See my update. – James Feb 11 '13 at 13:02
  • I don't get it, how can the Json be invalid ? How can I send it then ? – Sana Joseph Feb 11 '13 at 13:05
  • 1
    @SanaJoseph "*how can the JSON be invalid?*" - pretty easily actually and not always immediately obvious. You aren't actually passing `JSON` to the server with the way your sending it, your passing a javascript object (which is completely different). You need to call `stringify` on the `postData` parameter itself. – James Feb 11 '13 at 13:10
  • But when I changed it to: data: JSON.stringify(postData). The issue was still not fix, is there another method to send the data ? – Sana Joseph Feb 11 '13 at 13:20
  • @SanaJoseph Could you post an example of the JSON you are sending to the server? Your server is expecting JSON but isn't getting valid JSON so is unable to parse it. For example, change your code to be: `data: JSON.stringify({ Function: 'MyFunc', RData: 'test' }),` - it should work fine. My guess is your `GetRData` isn't actually returning valid JSON. – James Feb 11 '13 at 13:32
  • That's what I send : RData = {"Name": "Data Club", "Goal": "Being A True developer","Activities": "Practicing All Stuff"}; – Sana Joseph Feb 11 '13 at 13:34
  • @SanaJoseph could you post the full `postData` data that is being sent? That section of JSON is valid, however, that's just a portion of it. – James Feb 11 '13 at 13:48
  • I tried : data: JSON.stringify({ "Function": 'MyFunc', "Club": 'test' }), & data: JSON.stringify({ Function: 'MyFunc', Club: 'test' }), but both didn't work. – Sana Joseph Feb 11 '13 at 13:49
  • 1
    So if you change the content-type to be `application/json` does it work? You are telling the server your sending JSONP but really your just sending JSON. For JSONP, you generally append a `callback` parameter to the URL e.g. `/Services/GetRData.aspx?callback=AddData` – James Feb 11 '13 at 13:52