3

I currently have some jquery that is POSTing data onto one of my web pages.

Right now I'm just trying to get it to post some JSON to test it out, but I can't figure out have to actually get the data in my back-end once it's posted.

I've always used Request.Params to get posted data, but it doesn't seem to be working this time.

This is the code I'm using to do the post:

// This data is just for testing purposes, doesn't actually do anything
var person = {
    name: "Bob",
    address: "123 Main St.",
    phone: "555-5555"
}

var jqxhr = $.ajax({
    type: "POST",
    url: "/example/mypage.aspx",
    contentType: 'application/json; charset=utf-8',
    dataType: "json",
    timeout: 0,
    success: function () {
        alert("Success");
    },
    error: function (xhr, status, error) {
        alert(error);
    },
    data: person
});

The post is definitely successful though, as I can see it using Fiddler, plus when I check Request.ContentLength it returns the right number of bytes that was posted.

But I can't find the actual data anywhere. Any ideas on what I'm doing wrong?

Thanks in advance.

4
  • data is your data - had the same issue a few days ago: stackoverflow.com/questions/29132493/… Commented Mar 20, 2015 at 21:18
  • I need to access the data in asp.net after it's posted to my back-end service, though. The data object is just what I used to post the json data. Commented Mar 20, 2015 at 21:21
  • Oh I see now - would probably look into changing your data variable name, not sure if it's messing with things. In any event, unsure how to accomplish what you are looking for - however, take a look here: msdn.microsoft.com/en-us/library/aa530946.aspx Commented Mar 20, 2015 at 21:25
  • Yeah, I'll change the variable name so it's more clear. Thanks Commented Mar 20, 2015 at 21:26

1 Answer 1

8

Posting javascript object:

  1. pass the plain object to the data option,
  2. leave the contentType option alone. The default option is perfect.

Then you can access the property values of the object in the Request collection as if you have posted a form.

server side:

   string input;
    using(var reader = new StreamReader(Request.InputStream)){
            input = reader.ReadToEnd();
        }

Posting Json:

  1. data: JSON.stringify(person),
  2. contentType: "application/json"

server side:

string json;
using(var reader = new StreamReader(Request.InputStream)){
        json = reader.ReadToEnd();
    }
var person = Json.Decode(json);

Referenced from: http://www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages

Sign up to request clarification or add additional context in comments.

6 Comments

How would I do this in Web Forms? I didn't give any back-end code just cause there really wasn't anything to look at. I was just using Request.Params, and putting a breakpoint in so I could fiddle around using the Immediate Window to try and find my data.
i am not sure about webforms but try adding data: JSON.stringify(data) in your ajax. and in codebehind somethign like this NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll=Request.Form;
You might want to use webservices if that does not work. Sorry for not being much help
Hmm, still not getting anything. Thanks though. If I used something other than JSON, would I be able to use Request.Params like usual? Just wondering if there's any kind of work around.
Hi Jeremy, So if you want to post a JavaScript object via jQuery, pass the plain object to the data option, and leave the contentType option alone. The default option is perfect. Then you can access the property values of the object in the Request collection as if you have posted a form. mikesdotnetting.com/article/220/…
|

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.