Jquery set header in mail request

I need to simulate this curl command

curl -i -H "Accept: application/json" -H "Content-type:application/json" -X POST -d '{"username":"pippo","password":"secret123"}' http://url.org/api/login

through jquery i did this way

$( document ).ready(function() {
    $.ajax({
      url:"http://urlapi/user/login",
      type:"POST",
      headers: { 
        "Accept" : "application/json; charset=utf-8",
        "Content-Type": "application/json; charset=utf-8"
      },
      data:{ username: "pippo", password: "secret123" },
      dataType:"json"
    })  
});

I still have text text / html. Correctly?

+4
source share
4 answers

Seems to work for me ... Are you sure you are looking for the right query? Take a look at the HTTP request in the following JSFiddle; it really contains the title Content-Type: http://jsfiddle.net/KqGY4/1/

$( document ).ready(function() {
    $.ajax({
      url:"http://fiddle.jshell.net/user/login",
      type:"POST",
      headers: { 
        "Accept" : "application/json; charset=utf-8",
        "Content-Type": "application/json; charset=utf-8"
      },
      data:{ username: "pippo", password: "secret123" },
      dataType:"json"
    })  
});
+2
source

Try beforeSend in your jQuery AJAX call:

$( document ).ready(function() {
    $.ajax({
      url:"http://urlapi/user/login",
      type:"POST",
      beforeSend: function(xhr){
                xhr.setRequestHeader("Content-Type","application/json");
                xhr.setRequestHeader("Accept","application/json");
      },
      data:{ username: "pippo", password: "secret123" },
      dataType:"json"
    })  
});
+2
source

contentType?

$( document ).ready(function() {
    $.ajax({
      url:"http://urlapi/user/login",
      type:"POST",
      headers: { 
        "Accept" : "application/json; charset=utf-8"
      },
      contentType:"application/json; charset=utf-8",
      data:{ username: "pippo", password: "secret123" },
      dataType:"json"
    })  
});
0

.post() →

$.ajaxSetup({
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
});

jquery , .

0

All Articles