Php jQuery和Ajax脚本不工作
因此,我目前正在尝试使用Jquery、curl、ajax和GoogleAPI实现货币转换脚本,但是我遇到了一些问题 这里是jquery+ajaxPhp jQuery和Ajax脚本不工作,php,jquery,ajax,curl,google-api,Php,Jquery,Ajax,Curl,Google Api,因此,我目前正在尝试使用Jquery、curl、ajax和GoogleAPI实现货币转换脚本,但是我遇到了一些问题 这里是jquery+ajax $(document).ready(function() { $("#convert").click(function () { var from = $("#from").val(); var to = $("#to").val(); var a
$(document).ready(function() {
$("#convert").click(function () {
var from = $("#from").val();
var to = $("#to").val();
var amount = $("#amount").val();
//Make data string
var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;
$.ajax({
type: "POST",
url: "conversion.php",
data: dataString,
success: function(data){
$('#result').show();
//Put received response into result div
$('#result').html(data);
}
});
});
});
下面是我在conversion.php中的内容
<?php
// sanitizing input using built in filter_input available from PHP 5.2
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_INT);
$from = filter_input(INPUT_POST, 'from', FILTER_SANITIZE_SPECIAL_CHARS);
$to = filter_input(INPUT_POST, 'to', FILTER_SANITIZE_SPECIAL_CHARS);
// building a parameter string for the query
$encoded_string = urlencode($amount) . urlencode($from) . '%3D%3F' . urlencode($to);
$url = 'http://www.google.com/ig/calculator?hl=en&amp;q=' . $encoded_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
$results = curl_exec($ch);
// this is json_decode function if you are having PHP < 5.2.0
// taken from php.net
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($results); $i++)
{
if (!$comment)
{
if ($results[$i] == '{') $out .= ' array(';
else if ($results[$i] == '}') $out .= ')';
else if ($results[$i] == ':') $out .= '=>';
else $out .= $results[$i];
}
else $out .= $results[$i];
if ($results[$i] == '"') $comment = !$comment;
}
// building an $x variable which contains decoded array
echo eval($out . ';');
echo $x['lhs'] . ' = ' . $x['rhs'];
我不确定这是否是ussue,但您构造URL以调用数据的方式是不正确的。你所拥有的是
$url = 'http://www.google.com/ig/calculator?hl=en&amp;q=' . $encoded_string;
这是不对的。注&;q
零件。您需要按如下方式更改代码:
$url = 'http://www.google.com/ig/calculator?hl=en&q=' . $encoded_string;
我不能肯定,但我不明白你为什么要回音eval($out.;)?只呼叫eval而不呼叫echo
使用php调用google的原因是跨域限制。但是,如果您正在服务器中加载json响应,则可以直接将json响应返回给jQuery。不需要在服务器上解析它。试试这个,让我们知道它是否有效:
<?php
// sanitizing input using built in filter_input available from PHP 5.2
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_INT);
$from = filter_input(INPUT_POST, 'from', FILTER_SANITIZE_SPECIAL_CHARS);
$to = filter_input(INPUT_POST, 'to', FILTER_SANITIZE_SPECIAL_CHARS);
// building a parameter string for the query
$encoded_string = urlencode($amount) . urlencode($from) . '%3D%3F' . urlencode($to);
$url = 'http://www.google.com/ig/calculator?hl=en&amp;q=' . $encoded_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
$results = curl_exec($ch);
header('Content-type: application/json');
echo $results;
// this should output the same data google sent, which is now in your domain so you get no cross domain errors
我修复了以下问题,并且在XAMPP1.7.7上启用了curl扩展,它对我有效
将您的查询更改为
$url = 'http://www.google.com/ig/calculator?hl=en&q=' . $encoded_string;
对每个键使用引号
if (!$comment)
{
if ($results[$i] == '{') $out .= ' array(\'';
else if ($results[$i] == '}') $out .= ')';
else if ($results[$i] == ',') $out .= ',\'';
else if ($results[$i] == ':') $out .= '\'=>';
else $out .= $results[$i];
}
如果#convert
是表单提交按钮,则阻止默认操作
$("#convert").click(function (event) {
event.preventDefault();
var from = $("#from").val();
// ...
});
现在发布amount=3&from=EUR&to=USD
返回3欧元=3.9792美元
我是stackoverflow新手,所以还不知道什么时候最好使用评论、新答案
但在此期间,经过深入研究,我在以下位置发现了一个非常简单的PHP货币转换脚本:
这正是我想要的,非常轻,完全符合我项目的需要。。。也许这对其他人也有帮助…你说“输出整个网页”是什么意思?它是conversion.php文件本身的内容吗?您也可以从浏览器或curl命令行尝试url“conversion.php”(以及params)以查看其是否正确。对不起,我指的是我正在测试脚本的网页,因此在#结果中它会显示整个网页(标题、导航菜单、内容等)。请在成功函数中尝试console.log(数据)。检查浏览器调试器(如Firebug)以查看从服务器发送的POST请求和接收的响应。这可能会有所帮助。不幸的是,这并没有解决问题,但仍然很好地发现了问题:-)谢谢你的回答,Juank,不幸的是,这没有产生任何结果。成功回调是否在ajax调用中触发?如果是这样,您在数据变量中得到了什么?
$("#convert").click(function (event) {
event.preventDefault();
var from = $("#from").val();
// ...
});