简体   繁体   English

将curl查询转换为jQuery.ajax()

[英]Convert a curl query to jQuery.ajax()

I have a curl command that work fine. 我有一个curl命令,可以正常工作。 It's a query to a coldfusion server that returns a WDDX : 这是对返回WDDX的Coldfusion服务器的查询:

curl --user 'adminUser:adminPass' 'http://blablabla/app.cfc?method=isSusbscribed&newsletterId=2222&language=en&email=anemal@mail.com'

The result is : 结果是:

<wddxPacket version='1.0'><header/><data><boolean value='true'/></data></wddxPacket>

This means that the user with the given email receive the newsletter with the id 2222. 这意味着具有给定电子邮件的用户会收到ID为2222的新闻通讯。

Now, I'm trying to write the same command but with Ajax in a js script : 现在,我尝试编写相同的命令,但在js脚本中使用Ajax:

I have tried to do this : 我试图做到这一点:

$.ajax({url:'http://blablabla/app.cfc?method=isSusbscribed&newsletterId=2222&language=en&email=anemal@mail.com'
,username:'adminUser'
,password:'adminPass'})
      .done(function(res){
        jRes=$(res).find('boolean[value]');
        if(jRes.length == 1) inputElem.prop('checked', jRes.attr('value')=='true'); 
      }).always(function(res,text){
        console.log(res);
        console.log(text);
      })
      ;

but it doesn't work. 但这不起作用。

My goal is to put a switch in the ON position if the user is registered to a newsletter. 我的目标是,如果用户注册了新闻通讯,则将开关置于ON位置。

Probably the syntax of the query is not correct and I think the problem is related to authentication. 查询的语法可能不正确,我认为问题与身份验证有关。

Can somebody help me? 有人可以帮我吗? Thanks 谢谢

Pretty sure when you do --user in a curl request then it's using Basic Authentication. 可以肯定的是,当您在curl请求中执行--user ,它将使用基本身份验证。 So you need to use Basic Authentication in your jQuery call. 因此,您需要在jQuery调用中使用基本身份验证。 You will also need to Base64 encode your username and password. 您还需要对Base64编码的用户名和密码进行编码。 Something like this: 像这样:

$.ajax({
  url:'...',
  beforeSend: function(xhr) { 
    xhr.setRequestHeader("Authorization", "Basic YWRtaW5Vc2VyOmFkbWluUGFzcw==");
  }
}).done(function(res){
  // handle success...
});

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

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