0

I have been trying to post JSON array to the server using angularjs. but its not reading the array part.I am able to send the non array part which are username,password and status with the following code

$http({
    method: 'POST',
    url: 'http://localhost:57302/api/Messages',
    data: {
        "Username": "SampleUsername",
        "Password": "SamplePassword",
        "Messages": [{
            "From": "Harsh",
            "to": "Rahul",
            "Body": "Hi there",
        }, {
            "From": "Harsh",
            "to": "Rahul",
            "Body": "How are you",
        }, {
            "From": "Rahul",
            "to": "Harsh",
            "Body": "I am Good",
        }],
        "Status": "SampleStatus",
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: function(obj) {
        var str = [];
        for (var p in obj)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    }
})

What changes are required in order to send the array part as well?

Harsh Vardhan
  • 111
  • 1
  • 14
  • why not use `Content-Type: application/json` without any transformations? – Girafa Aug 21 '15 at 09:40
  • I just modified that..it says XMLhttpRequest Invalid 400 – Harsh Vardhan Aug 21 '15 at 09:44
  • Seems like your server is not set up to accept `application/json` properly. – Girafa Aug 21 '15 at 09:45
  • But it works when using jquery and I was also able to post json objects but not arrays – Harsh Vardhan Aug 21 '15 at 09:46
  • Do you use jQuery? It has useful method `$.param(data)` which acts as your transformRequest but more properly. – Girafa Aug 21 '15 at 09:46
  • I really want to do it using pure angular since jquery is conflicting with ionic framework (the technology I am using for building my front end) – Harsh Vardhan Aug 21 '15 at 09:48
  • You can use workarond: `angular.toJson(data)` and `json_decode(data)` (php) on server – Girafa Aug 21 '15 at 09:50
  • But the most correct way is to use `application/json`. – Girafa Aug 21 '15 at 09:50
  • Or write your `transformRequest` function more properly (see how it's done in jQuery.param method). Now your function goes only one level deep within data structure. – Girafa Aug 21 '15 at 09:52
  • do I have to do somthing like [this](http://stackoverflow.com/questions/1714786/querystring-encoding-of-a-javascript-object)? – Harsh Vardhan Aug 21 '15 at 10:10
  • here, you do not post JSON but an url-form-encoded, transforming the JSON in a URLEncoded string. Why not sending JSON directly in the POST ? URLEncoding is usually used for GET request where you serialize data to be in the HTTP URL request. For post you can simply post json with correct content-type, and on server side, you read JSON data in the body of the request. – osallou Aug 21 '15 at 10:14
  • @osallou if I remove transformRequest and set Content-Type as application/json it gives me http status code 400 - XMLHttpRequest cannot load http://localhost:57302/api/Messages – Harsh Vardhan Aug 21 '15 at 10:24
  • but on server-side, do you read from the body of the message? – osallou Aug 22 '15 at 11:54
  • Whenn I send just a json object, I am able to recieve it on the server side but not json array – Harsh Vardhan Aug 24 '15 at 09:28

0 Answers0