【问题标题】:How to convert this curl to jQuery AJAX POST? [duplicate]如何将此 curl 转换为 jQuery AJAX POST? [复制]
【发布时间】:2016-10-08 09:04:26
【问题描述】:
curl --request POST \
--url 'https://usX.api.mailchimp.com/3.0/lists/1234/members' \
--user 'username:api_key' \
--header 'content-type: application/json' \
--data '{"email_address":"user@example.com", "status":"pending"}' \
--include

我已经使用 curl 并且当我尝试将订阅者添加到我的 Mailchimp 列表时它工作正常。

但是,我不确定如何将此 curl 转换为有效的 jQuery AJAX POST 请求,因为我只知道如何将 data 传递给 AJAX POST。

由于这个 curl 有 --user--header--include,我不知道如何构建正确的 AJAX。

请帮忙。

$.ajax({
    url : "https://usX.api.mailchimp.com/3.0/lists/1234/members",
    type: "POST",
    data: {email_address: "user@example.com", status: "pending"},
    // stuck here - what else should I put?
});

【问题讨论】:

  • 我看到 3 个答案有 3 个反对票,但没有解释是什么让他们错了。我会阅读 curl 的联机帮助页以了解这些属性的定义。你能至少评论一下他们为什么错了,所以它被认为是合理的吗?

标签: javascript jquery ajax curl


【解决方案1】:

这已经在下面的问题中得到了回答。

Converting curl cmd to jQuery $.ajax().

$.ajax({
    url: "https://usX.api.mailchimp.com/3.0/lists/1234/members",
    beforeSend: function(xhr) { 
      xhr.setRequestHeader("Authorization", "Basic " + btoa("username:api_key")); 
    },
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    processData: false,
    data: '{"email_address": "user@example.com", "status": "pending"}',
    success: function (data) {
      alert(JSON.stringify(data));
    },
    error: function(){
      alert("Cannot get data");
    }
});

【讨论】:

    【解决方案2】:

    这是一个关于使用 jQuery 发送用户/密码的问题/答案:

    How to use Basic Auth with jQuery and AJAX?

    使用 jQuery 的 beforeSend 回调添加带有身份验证信息的 HTTP 标头:

    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
    },
    

    你可以像这样设置内容类型:

    contentType: "application/json",
    

    --include 似乎是curl 的输出选项: 所有 HTTP 回复都包含一组通常隐藏的响应头,使用 curl 的 --include (-i) 选项来显示它们以及文档的其余部分。

    【讨论】:

      【解决方案3】:

      尝试以下方法:

      $.ajax({
          url : "https://usX.api.mailchimp.com/3.0/lists/1234/members",
          username:'username',//change this
          password:'api_key',//change this
          type: "POST",
          contentType: "application/json",
          data: {email_address: "user@example.com", status: "pending"},
          success: function(data){
               //your code to process output value from data
          }
      });
      

      或使用标题

      $.ajax({
              url : "https://usX.api.mailchimp.com/3.0/lists/1234/members",
              type: "POST",
              headers: {
                    'Authorization':'Basic ' + btoa("username:api_key"),
                    'Content-Type':'application/json'
              },
              data: {email_address: "user@example.com", status: "pending"},
              success: function(data){
                   //your code to process output value from data
              }
          });
      

      【讨论】:

        猜你喜欢
        • 2019-01-25
        • 2020-05-20
        • 2018-02-16
        • 2013-12-07
        • 2021-02-14
        • 2021-10-24
        • 1970-01-01
        • 2016-04-12
        • 1970-01-01
        相关资源
        最近更新 更多