I want to convert curl command :
curl -X POST -H "X-Requested-With: XMLHttpRequest" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"username": "admin","password": "admin"}' "http://localhost:8080/api/auth/login"
to ajax jquery, i have tried to use this code below but it does not work:
$.ajax({
type: 'POST',
crossDomain: true,
url: 'http://localhost:8080/api/auth/login',
dataType : 'json',
data: JSON.stringify(datax),
success:function(data){
console.log(data);
},
error: function(data) { // if error occured
var responseText = $.parseJSON(data.responseText);
if(responseText.error){
}
}
})
.done(function(data) {
})
dataType: 'json' does not reflects the contentType. It's for jQuery to know how to handle the answer.
You are looking for contentType...
$.ajax({
type: 'POST',
crossDomain: true,
cache: false,
url: 'http://localhost:8080/api/auth/login',
contentType : 'application/json',
data: JSON.stringify(datax) //Expecting datax is username & password
})
.done(function(data) {
})