2

I was trying to post an empty array using ajax call, but the data is posted as an empty string i.e. Parts: . I want the data to be posted as Parts:[] which is not happening because the webservice handles empty collection only ie Parts[].

Code:
var parts=[];
$.ajax({
type: "POST",
url: "http://api-test.example.com",
dataType: "json",
headers:{Authorization:authHeader},
data: "Parts="+parts,
    success: function (result) {
    },
error: function (result){
}
});

Any idea of how to fix this?

user3554548
  • 85
  • 2
  • 7
  • most probably you should handle this on the server side, what is the underlying technology for your webservice? – amd Apr 28 '14 at 07:09

3 Answers3

0

Try to pass data as data : {}

$.ajax({
type: "POST",
url: "http://api-test.example.com",
dataType: "json",
headers:{Authorization:authHeader},
data: {Parts : parts} ,
    success: function (result) {
    },
error: function (result){
}
});
sshet
  • 1,152
  • 1
  • 6
  • 15
0

Set below property to your call,

 'traditional': true

also edit your JSON data pass as,

 data: JSON.stringify({ "parts" : parts } );

Actually, All previous versions used to pass arrays by this way, but as new frameworks ruby on rails and others process arrays differently jQuery started new approach.

More help on SO is as below,

What is "traditional style of param serialization' in JQuery

Community
  • 1
  • 1
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62
0

As the "traditional: true" was not working for me, i found an other solution : setting the array to null.

let myArray = [];

// whatever code

if(myArray == [])
  myArray = null;
Adrien C
  • 364
  • 2
  • 11