Reading data with C #

I am using Jquery Ajax function to post data from html page and I am reading from server from C #.

When I used Get, the data was sent successfully, but when I switched to Post, the data value on the backend will be null.

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!');
                }
            });
        }

      

FROM#:

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

      

So what am I doing wrong and how can I fix it?

Update:

I just deleted

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

      

and it worked.

+3


source to share


1 answer


Assuming the above code is exactly what you are doing, a typo appears:

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

      

Your variable Data

, but your call to JSON.Stringify

on RData

that was not declared (hence the property is null).



Update

Based on your last comment, I would say this is a JSON formatting issue. This can happen when you are trying to manually construct JSON objects that you partially render 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),
    ...
});

      

+1


source







All Articles