简体   繁体   English

在jquery ajax请求中转换curl查询

[英]converting curl query in jquery ajax request

I have a curl command for getting json data and I want to send this request from javascript program. 我有一个curl命令来获取json数据,我想从javascript程序发送此请求。 Please anybody can tell how to convert this curl command into jquery ajax request. 请任何人都可以告诉我如何将此curl命令转换为jquery ajax请求。 Command: 命令:

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

cURL -H parameters add extra headers to the request. cURL -H参数将额外的标头添加到请求中。

jQuery.ajax have an option headers , this is a Plain JavaScript Object with the pair name / value for each header. jQuery.ajax有一个选项headers ,这是一个普通JavaScript对象 ,每个标头具有对名称 /

Then, your cURL request will translate in something like this: 然后,您的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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM