【问题标题】:converting curl query in jquery ajax request在 jquery ajax 请求中转换 curl 查询
【发布时间】:2015-09-17 10:03:17
【问题描述】:

我有一个获取 json 数据的 curl 命令,我想从 javascript 程序发送这个请求。请任何人都可以告诉如何将此 curl 命令转换为 jquery ajax 请求。 命令:

curl -H "Snapdeal-Affiliate-Id:<your affiliate ID>" -H "Snapdeal-Token-Id:<your affiliate Token>" "<URL for the chosen category>" -H "Accept:application/json"

【问题讨论】:

    标签: javascript jquery json ajax curl


    【解决方案1】:

    cURL -H parameters 为请求添加额外的标头。

    jQuery.ajax 有一个选项 headers,这是一个 Plain JavaScript 对象,每个标头都有一对 name/value

    然后,您的 cURL 请求将转换为如下内容:

    $.ajax(url, {
      url: "<URL for the chosen category>"
      headers: {
        "Snapdeal-Affiliate-Id":"<your affiliate ID>",
        "Snapdeal-Token-Id":"<your affiliate Token>"
      },
      accepts: "application/json"
    }).done(function (data) {
      // code to handle succesful response
    }).fail(function (data) {
      // code to handle error response
    });
    

    【讨论】:

      猜你喜欢
      • 2018-02-16
      • 2017-04-02
      • 1970-01-01
      • 2015-10-06
      • 2016-10-10
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多