在javascript中执行curl请求? [英] Perform curl request in javascript?
问题描述
是否可以在jQuery或javascript中发送curl请求?
Is it possible to send a curl request in jQuery or javascript?
这样的事情:
curl \
-H 'Authorization: Bearer 6Q************' \
'https://api.wit.ai/message?v=20140826&q='
因此,在提交表格的PHP中,像这样:
So, in PHP on submission of a form, like this:
$header = array('Authorization: Bearer 6Q************');
$ch = curl_init("https://api.wit.ai/message?q=".urlEncode($_GET['input']));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
我要做的是执行此卷曲请求,返回json然后我计划用jQuery的 $。get()
函数解析它。
What I'm trying to do is perform this curl request, which returns json and then I plan on parsing it with jQuery's $.get()
function.
推荐答案
curl是一个命令(在Linux和PHP中)。 Curl通常会发出HTTP请求。
curl is a command (in linux and PHP). Curl typically makes an HTTP request.
您真正想做的是从javascript发出HTTP(或XHR)请求。
What you really want to do is make an HTTP (or XHR) request from javascript.
使用这个词汇,你会发现一堆例子,对于初学者来说:
使用jquery和ajax发送授权标头
Using this vocab you'll find a bunch of examples, for starters: Sending authorization headers with jquery and ajax
基本上你要调用 $。ajax
有一些标题选项等。
Essentially you will want to call $.ajax
with a few options for the header, etc.
$.ajax({
url: 'https://api.wit.ai/message?v=20140826&q=',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer 6QXNMEMFHNY4FJ5ELNFMP5KRW52WFXN5")
}, success: function(data){
alert(data);
//process the JSON data etc
}
})
这篇关于在javascript中执行curl请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!