i need send mi post in this format
{
"elements" : ["elem1", "elem2","elem3" ]
}
and i have created this array :
var miElem=["elem1", "elem2","elem3" ]
$.post({
url: 'url',
data: {"elements" : miElem} ,
dataType: 'json',
success: function(data) {
console.log(data)
}.bind(this),
error: function(xhr, status, err) {
console.log(err)
}.bind(this)
});
but dont work, the data send well, but the format is incorrect
Use $.ajax()
like this.The use of $.post
is not correct.Check the syntax for $.post
.here https://api.jquery.com/jquery.post/
var miElem=["elem1", "elem2","elem3"];
$.ajax({
url: 'url',//give your url here
type:'POST',
data: {elements : miElem} ,
dataType: 'json',
success: function(data) {
alert('success');//test for successful ajax request
console.log(data);
},
error: function(xhr, status, err) {
alert('error');//test for unsuccessful ajax request
console.log(err);
}
});
You should used $.ajax
instead of $.post
. In $.ajax
don't use success and error parameters. Use .done()
, .fail()
, .. (http://api.jquery.com/jquery.ajax/) it's more comprehensive. You can use .then()
if you want to use promise.
Datatype parameter is for response type.
Your request is failing because
$.post()
method.type:'POST'
if you intended to use $.ajax
.for $.post()
do the following:
$.post('url', {elements: miElem}).done(function(data) {
console.log(data);
}).fail(function (error)
console.error(error);
});
for $.ajax
do the following:
$.ajax({
type: 'POST',
url: 'url',
data: {elements: miElem},
success: function (data) {
console.log(data);
},
dataType: 'json'
});