【问题标题】:Convert curl to jquery post with json payload使用 json 有效负载将 curl 转换为 jquery 帖子
【发布时间】:2013-08-27 10:56:30
【问题描述】:

我正在尝试“转换”以下工作卷曲:

curl -X POST -H "Content-Type: application/json" -d
    "{"""username""":"""myuser""", """password""":"""nastypassword"""}"
    http://website.com/api/v1.0/login

到 jquery 调用。

最重要的是,在服务器端我需要获取 json 数据,因为我正在使用烧瓶和request.json 来获取数据。

我尝试了以下多种变体:

$.ajax('http://website.com/api/v1.0/login', {
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json', // I don't think this needs to be here because of dataType
    data: {
        username: 'myuser',
        password: 'nastypassword'
    }
})
    .done(function(data) {
        console.log(data);
    })
    .fail(function(xhr, status, error) {
        alert(status + ' - ' + error);
    });

如何让 jquery 发送 json?

我可能应该补充一点,我正在创建一个 cordova 应用程序。并且access origin 设置为*GET 工作正常。

【问题讨论】:

    标签: jquery json post curl


    【解决方案1】:

    你可能想要

    $.ajax('http://website.com/api/v1.0/login', {
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json', // I don't think this needs to be here because of dataType
      data: {
        username: 'myuser',
        password: 'nastypassword'
      },
      success: function(data){
        console.log(data);
      },
      error: function(xhr, status, error){
        alert(status + ' - ' + error);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2021-10-05
      相关资源
      最近更新 更多