2

I am trying to post key-value pair data from angular application to a asp.net page however I am not able to receive the result at the asp.net page.

Example Data

 {fullName: "Vikas Bansal", userId: "1001387693259813", userimageurl: "http://graph.facebook.com/1001387693259813/picture?width=250&height=250"}

Angular Code

    var postData = {
        fullName: $scope.name,
        userId: $scope.userId,
        userimageurl: $scope.userImage
    };

    var postUrl = 'http://localhost:41115/Index.aspx';

    $http({
        url: postUrl,
        method: "POST",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: postData
    }).then(function (responce) {
        console.log(responce);
    }, function (responce) {
        console.log(responce);
    });

Asp.net Page Code

protected void Page_Load(object sender, EventArgs e)
{
    var userId = Request.Form["userId"];    /// getting null
    var fullName = Request.Form["fullName"];    /// getting null
    var img = Request.Form["userimageurl"]; /// getting null
}

In asp.net page all variables are null.

enter image description here

EDIT

By changing content type to 'Content-Type': 'application/json; charset=utf-8' in angularJs post request

and by using var strJSON = (new StreamReader(Request.InputStream).ReadToEnd()); I am able get the posted json

but I am still wondering if there is a way to get key-value pairs directly?

like this var userId = Request.Form["userId"];

2
  • You have content type mismatch. Your payload is JSON but you told ASP that it's Form. Set your Content-Type to application/json; charset=utf-8. Commented Apr 22, 2016 at 17:25
  • @KKKKKKKK used this but did not receive any value Commented Apr 22, 2016 at 17:27

1 Answer 1

2

It's because your payload is actually JSON but you tried to pass as a FormData object.

You'll need to make a FormData() object. Then

(1) Tell AngularJs not to serialize it by using the angular.identity function.

(2) Angular's default header for PUT and POST requests is application/json, so you'll want to override that too. The browser sets the Content-Type to multipart/form-data and fills in the correct boundary.

Try this:

var postData = new FormData();
postData.append("fullName", $scope.name);
postData.append("userId", $scope.userId);
postData.append("userimageurl", $scope.userImage);

$http.post(postUrl, postData, {
    transformRequest: angular.identity,
    headers: { 'Content-Type': undefined }
}).then(function(data) {
    //success
}, function(error) {
    //error
});
Sign up to request clarification or add additional context in comments.

1 Comment

exactly what I needed. I request you to please provide some details on transformRequest: angular.identity, and why did you set 'Content-Type': undefined . Many many thanks

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.