衫裤跑路 2017-01-04 10:05 采纳率: 50%
浏览 94

将curl命令转换为Ajax

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) {

})
  • 写回答

1条回答 默认 最新

  • weixin_33701251 2017-01-04 10:13
    关注

    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) {
    })
    

    http://api.jquery.com/jquery.ajax/

    评论

报告相同问题?

悬赏问题

  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题