简体   繁体   English

使用ajax发出curl请求

[英]Using ajax to make a curl request

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. 基本上,由于身份验证,我需要发布curl,而不仅仅是发布URL。

The example works fine with the simple url, but what I am wanting to use for 'the_url' is this: 该示例使用简单的url可以正常工作,但是我想为'the_url'使用的是:

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? 解决此问题的最佳方法是什么,如果无法解决,可以推荐最佳解决方法吗?

Thanks in advance. 提前致谢。

You need a page on your server that the JavaScript points at, which has the curl command in it. 您需要在服务器上的页面指向JavaScript指向的页面,其中包含curl命令。 You can't just run cURL directly as a url like that. 您不能像这样直接将cURL直接作为URL运行。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $the_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

If you were using simple AJAX, then you could just add custom (basic authentication) header at the begining of your AJAX call. 如果您使用的是简单的AJAX,则只需在AJAX调用开始时添加自定义(基本身份验证)标头。 But you are using JSONP, which actually is equivalent to adding <script> tag to your HTML. 但是您使用的是JSONP,实际上等效于在HTML中添加<script>标记。

So what you are asking is: can I do basic authentication with <script> tag? 所以,您要问的是:我可以使用<script>标签进行基本身份验证吗? No, you can't. 不,你不能。

The only possibility left for you is to do server side processing, like Matt Gibson suggested. 剩下的唯一可能性就是执行服务器端处理,如Matt Gibson建议的那样。

Note however, that the browser should fire authentication dialog after the JSONP request (also see this answer ). 但是请注意,浏览器应在JSONP请求之后触发身份验证对话框(另请参见此答案 )。 Isn't it working in your case?? 这不是您的情况吗?

Update your jquery ajax call to include the username and passward options for HTTP authentication, see the Advanced options in the documentation: 更新您的jquery ajax调用,以包括用于HTTP身份验证的usernamepassward选项,请参阅文档中的“ 高级”选项

 If the server performs HTTP authentication before providing a response, the user name and password pair can be sent via the username and password options. 

Update as pointed out in the comments, HTTP authentication simply doesn't work with JSONP , as the questioner used (didn't see that, my bad). 正如评论中指出的那样进行更新 ,因为使用了发问者,HTTP身份验证根本无法与JSONP一起使用(我没看到这一点)。 I'll leave this answer here anyway for other people to find that don't use JSONP. 无论如何,我还是将这个答案留在这里,以供其他人发现不使用JSONP。

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

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