使用php和jquery将Curl转换为Ajax
我只是想问。。。如何转换此代码:使用php和jquery将Curl转换为Ajax,php,jquery,ajax,curl,Php,Jquery,Ajax,Curl,我只是想问。。。如何转换此代码: $url='https://www.zopim.com/api/v2/agents'; $username="boom"; $password="baam"; // chat user $ch1 = curl_init(); curl_setopt($ch1, CURLOPT_URL, $url); curl_setopt($ch1, CURLOPT_USERPWD, "$username:$passwor
$url='https://www.zopim.com/api/v2/agents';
$username="boom";
$password="baam";
// chat user
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$output1 = curl_exec($ch1);
$info1 = curl_getinfo($ch1);
curl_close($ch1);
$userinfo = json_decode($output1);
到ajax。。。我想自动调用json数据。。。希望你能在这方面帮助莫。。谢谢。您可以使用$.ajax或$.post来实现这一点。下面是一个例子 PHP脚本中的
<?php
$url='https://www.zopim.com/api/v2/agents';
$username="boom";
$password="baam";
?>
<script type="text/javascript">
// Using the core $.ajax() method
$.ajax({
// The URL for the request
url: "<?php echo $url; ?>",
// The data to send (will be converted to a query string)
data: {
username: "<?php echo $username; ?>",
password: "<?php echo $password; ?>"
},
// Whether this is a POST or GET request
type: "POST",
// The type of data we expect back
dataType : "json",
})
// Code to run if the request succeeds (is done);
// The response is passed to the function
.done(function( json ) {
//Hurray!! here is your json data
console.log(json.id);
console.log(json.title);
})
// Code to run if the request fails; the raw request and
// status codes are passed to the function
.fail(function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
})
// Code to run regardless of success or failure;
.always(function( xhr, status ) {
alert( "The request is complete!" );
});
</script>
在JavaScript中
<?php
$url='https://www.zopim.com/api/v2/agents';
$username="boom";
$password="baam";
?>
<script type="text/javascript">
// Using the core $.ajax() method
$.ajax({
// The URL for the request
url: "<?php echo $url; ?>",
// The data to send (will be converted to a query string)
data: {
username: "<?php echo $username; ?>",
password: "<?php echo $password; ?>"
},
// Whether this is a POST or GET request
type: "POST",
// The type of data we expect back
dataType : "json",
})
// Code to run if the request succeeds (is done);
// The response is passed to the function
.done(function( json ) {
//Hurray!! here is your json data
console.log(json.id);
console.log(json.title);
})
// Code to run if the request fails; the raw request and
// status codes are passed to the function
.fail(function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
})
// Code to run regardless of success or failure;
.always(function( xhr, status ) {
alert( "The request is complete!" );
});
</script>
//使用核心$.ajax()方法
$.ajax({
//请求的URL
url:“”,
//要发送的数据(将转换为查询字符串)
数据:{
用户名:“”,
密码:“
},
//这是一个POST还是GET请求
类型:“POST”,
//我们期望返回的数据类型
数据类型:“json”,
})
//请求成功(完成)时要运行的代码;
//响应被传递给函数
.done(函数(json){
//万岁!!这是你的json数据
log(json.id);
log(json.title);
})
//请求失败时要运行的代码;原始请求和
//状态代码被传递给函数
.失败(功能(xhr、状态、错误抛出){
警报(“抱歉,出现问题!”);
log(“错误:+errorshown”);
控制台日志(“状态:+状态”);
console.dir(xhr);
})
//无论成功或失败都要运行的代码;
.始终(功能(xhr、状态){
警报(“请求已完成!”);
});
您可以使用$.ajax或$.post来实现这一点。下面是一个例子
PHP脚本中的
<?php
$url='https://www.zopim.com/api/v2/agents';
$username="boom";
$password="baam";
?>
<script type="text/javascript">
// Using the core $.ajax() method
$.ajax({
// The URL for the request
url: "<?php echo $url; ?>",
// The data to send (will be converted to a query string)
data: {
username: "<?php echo $username; ?>",
password: "<?php echo $password; ?>"
},
// Whether this is a POST or GET request
type: "POST",
// The type of data we expect back
dataType : "json",
})
// Code to run if the request succeeds (is done);
// The response is passed to the function
.done(function( json ) {
//Hurray!! here is your json data
console.log(json.id);
console.log(json.title);
})
// Code to run if the request fails; the raw request and
// status codes are passed to the function
.fail(function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
})
// Code to run regardless of success or failure;
.always(function( xhr, status ) {
alert( "The request is complete!" );
});
</script>
在JavaScript中
<?php
$url='https://www.zopim.com/api/v2/agents';
$username="boom";
$password="baam";
?>
<script type="text/javascript">
// Using the core $.ajax() method
$.ajax({
// The URL for the request
url: "<?php echo $url; ?>",
// The data to send (will be converted to a query string)
data: {
username: "<?php echo $username; ?>",
password: "<?php echo $password; ?>"
},
// Whether this is a POST or GET request
type: "POST",
// The type of data we expect back
dataType : "json",
})
// Code to run if the request succeeds (is done);
// The response is passed to the function
.done(function( json ) {
//Hurray!! here is your json data
console.log(json.id);
console.log(json.title);
})
// Code to run if the request fails; the raw request and
// status codes are passed to the function
.fail(function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
})
// Code to run regardless of success or failure;
.always(function( xhr, status ) {
alert( "The request is complete!" );
});
</script>
//使用核心$.ajax()方法
$.ajax({
//请求的URL
url:“”,
//要发送的数据(将转换为查询字符串)
数据:{
用户名:“”,
密码:“
},
//这是一个POST还是GET请求
类型:“POST”,
//我们期望返回的数据类型
数据类型:“json”,
})
//请求成功(完成)时要运行的代码;
//响应被传递给函数
.done(函数(json){
//万岁!!这是你的json数据
log(json.id);
log(json.title);
})
//请求失败时要运行的代码;原始请求和
//状态代码被传递给函数
.失败(功能(xhr、状态、错误抛出){
警报(“抱歉,出现问题!”);
log(“错误:+errorshown”);
控制台日志(“状态:+状态”);
console.dir(xhr);
})
//无论成功或失败都要运行的代码;
.始终(功能(xhr、状态){
警报(“请求已完成!”);
});
js代码在哪里,您尝试了什么?我想您希望此代码自动加载?如果是,请将此代码放入一个文件中,并在特定时间间隔内使用ajax调用该文件。@madalinivascu json位于链接上。json代码在哪里,您尝试了什么?我想您希望此代码自动加载?如果是,请将此代码放在一个文件中,并在特定时间间隔内使用ajax调用该文件。@madalinivascu json位于链接上。我尝试了此操作,但出现错误<代码>XMLHttpRequest无法加载https://www.zopim.com/api/v2/agents. 请求的资源上不存在“Access Control Allow Origin”标头。起源'http://localhost因此,不允许访问。响应的HTTP状态代码为401。我尝试了此操作,但出现错误<代码>XMLHttpRequest无法加载https://www.zopim.com/api/v2/agents. 请求的资源上不存在“Access Control Allow Origin”标头。起源'http://localhost因此,不允许访问。响应的HTTP状态代码为401。