使用Ajax使卷曲的请求卷曲、Ajax

2023-09-10 21:04:06 作者:∝、戒烟.戒不掉思恋

这回答帮助我基本上但还没有完全解决我的问题。我使用code相似,那就是:

This answer has helped me largely but hasn't fully resolved my issues. I'm using code similar to that, which is:

function getResponse() {

    var the_url = "http://www.myurl.com/api/get";

    $.ajax({
      url: the_url,
      dataType: 'jsonp',
      type: 'get',
      success: function(data) {
        var json_response = data;
        alert(data);
      }
    });
}

基本上,而不是只发布一个网址,我需要后,因为认证的卷曲。

Basically, instead of just posting a url I need to post a curl because of authentication.

这个例子正常工作与简单的URL,但是我想使用的'the_url'是这样的:

The example works fine with the simple url, but what I am wanting to use for 'the_url' is this:

var the_url = 'curl -u username:password "http://www.myurl.com/api/get"';

这是我在哪里有问题,这意味着成功的语句永远不会达到。

This is where I am having issues which means the success statement is never reached.

什么是解决这一问题的最佳途径,如果它不是实现可能有人建议最好的解决办法?

What is the best way to resolve this, and if it's not achievable could someone recommend the best workaround?

在此先感谢。

推荐答案

如果你使用简单的AJAX,那么你可能就在你的AJAX调用的开头添加自定义(基本身份验证)的头。但是,你正在使用JSONP,这实际上相当于增加<脚本> 标记的HTML

If you were using simple AJAX, then you could just add custom (basic authentication) header at the begining of your AJAX call. But you are using JSONP, which actually is equivalent to adding <script> tag to your HTML.

所以,你问的是:我可以做&LT基本身份验证;脚本&GT; 标签?不,你不能。

So what you are asking is: can I do basic authentication with <script> tag? No, you can't.

唯一的可能留给你是做服务器端处理,像马特·吉布森建议。

The only possibility left for you is to do server side processing, like Matt Gibson suggested.

请注意但是,浏览器应该在JSONP请求后触发身份验证对话框(参见this回答)。是不是在你的情况下工作?

Note however, that the browser should fire authentication dialog after the JSONP request (also see this answer). Isn't it working in your case??