Javascript 使用 ajax 发出 curl 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11312908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using ajax to make a curl request
提问by Lloyd Powell
This answerhas 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.
提前致谢。
回答by Matt Gibson
You need a page on your server that the JavaScript points at, which has the curl command in it. You can't just run cURL directly as a url like that.
您需要在您的服务器上有一个 JavaScript 指向的页面,其中包含 curl 命令。您不能像那样直接将 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;
回答by freakish
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.
如果您使用的是简单的 AJAX,那么您只需在 AJAX 调用开始时添加自定义(基本身份验证)标头即可。但是您使用的是 JSONP,这实际上相当于<script>
在您的 HTML 中添加标签。
So what you are asking is: can I do basic authentication with <script>
tag? No, you can't.
所以您要问的是:我可以使用<script>
标签进行基本身份验证吗?不,你不能。
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). Isn't it working in your case??
但是请注意,浏览器应在 JSONP 请求之后触发身份验证对话框(另请参阅此答案)。它在你的情况下不起作用吗?
回答by catchmeifyoutry
Update your jquery ajaxcall to include the username
and passward
options for HTTP authentication, see the Advanced optionsin the documentation:
更新您的jquery ajax调用以包含HTTP 身份验证的username
和passward
选项,请参阅文档中的高级选项:
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.
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.
Updateas pointed out in the comments, HTTP authentication simply doesn't work with JSONP, as the questioner used (didn't see that, my bad). I'll leave this answer here anyway for other people to find that don't use JSONP.
正如评论中指出的那样更新,HTTP 身份验证根本不适用于 JSONP,正如提问者所使用的那样(没看到,我的错)。无论如何,我都会把这个答案留在这里,让其他人发现不使用 JSONP。