我正试图从这个json javascript代码中获取一些数据,并使用CURL 将它们打印到PHP
<html>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<body>
<script type="text/javascript">
jQuery.ajax(
{
type:"POST",
contentType:"application/json;charset=utf-8",
url:"https://www.bancopromerica.com.gt/wsservicebus/wsonlineservicebus.asmx/getTipoCambio",
data:"{}",
dataType:"json",
async: false,
success: function(msg) {
a("#compInter",msg.d.compraInternet); //Compra Internacional
a("#ventInter",msg.d.ventaInternet); //Venta Internacional
a("#compAgencia",msg.d.compraAgencia); //Compra Agencia
a("#ventAgencia",msg.d.ventaAgencia); //Venta Agencia
},
error: function(textStatus, errorThrown, errorDetail){
alert(errorDetail);
}
});
function a(a,b)
{
jQuery(a).append(b);
}
</script>
</body>
</html>
我收到这个错误:
[异常…"失败"nsresult:"0x80004005(NS_ERROR_Failure)"location:"JS框架::http://code.jquery.com/jquery-1.12.0.min.js::.发送::第4行"数据:否]
你知道如何在PHP中正确地做到这一点吗?
我认为您所有的JQuery.ajax
都是正确的(顺便说一下,您可以将其更改为$.ajax
)。我想的问题是在你的a
函数中。这就是我认为它应该是什么样子,以及我是如何做到的:
function a(a, b)
{
$(a).html(b);
}
我还没有在codepin、jsfiddle或任何东西上检查过,但我认为这会奏效。
一个非常基本的php-cURL示例是:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://www.bancopromerica.com.gt/wsservicebus/wsonlineservicebus.asmx/getTipoCambio");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
echo json_encode($output)
您需要var_dump
和output
,因为您希望分配适合您需要的数组键/值。