2012-01-28 135 views
0

所以我目前正在尝试使用jquery,curl,ajax和Google API来实现货币转换脚本,但是我遇到了一些问题。jQuery和Ajax脚本不工作

因此,这里是jQuery的AJAX +

$(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;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']; 

现在的问题是,当我点击转换按钮,它也会输出全#results div中的网页,而不是来自conversion.php的$ x

我已经花了整整一天的时间,所以任何帮助,非常感谢。

仅供参考 - 卷曲已安装并正常工作

+0

你说什么“输出整个网页”是什么意思?它是conversion.php文件本身的内容吗?您也可以从浏览器或curl命令行尝试url“conversion.php”(以及params),看看它是否正常工作。 – rsmoorthy 2012-01-28 17:33:59

+0

对不起,我指的是我正在测试脚本的网页,因此在#result中显示整个网页(标题,导航菜单,内容等)。 – Danny 2012-01-28 17:40:42

+0

在您的成功函数中尝试console.log(数据)。检查您的浏览器调试器(如Firebug)以查看从服务器收到的POST请求和响应。这可能会有所帮助。 – rsmoorthy 2012-01-28 17:49:55

回答

0

不能肯定地说,但我不明白为什么你回荡的eval($了“;”。)?仅在没有回声的情况下调用eval。

使用php来拨打谷歌电话的原因是由于跨域限制。但鉴于你正在你的服务器中加载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;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 

然后在jQuery的加载响应

$.ajax({ 
     type: "POST", 
     url: "conversion.php", 
     data: dataString, 
     dataType:"json", 
     success: function(data){ 
      // If I load this page http://www.google.com/ig/calculator?hl=en&q=1USD=?EUR 
      // I get {lhs: "1 U.S. dollar",rhs: "0.757174226 Euros",error: "",icc: true} 
      // Assuming your call is correct and you have the same response you can now 
      // access the data like so: 


     $('#result').show(); 

     //Put received response into result div 
     $('#result').html(data.lhs + ' is equivalent to ' + data.rhs + ' Today'); 
     } 
    }); 
+0

感谢您的反应Juank,不幸的是没有产生任何结果。 – Danny 2012-01-28 17:52:46

+0

是在ajax调用中成功回调触发吗?如果是这样,你在数据变量中得到了什么? – Juank 2012-01-28 18:01:12

1

嗯,我不知道这是一个ussue,但在构造URL来调用数据的方式不正确。你有什么是

$url = 'http://www.google.com/ig/calculator?hl=en&amp;amp;q=' . $encoded_string; 

这是不正确的。注意&amp;q部分。你需要改变你的代码如下:

$url = 'http://www.google.com/ig/calculator?hl=en&q=' . $encoded_string; 
+0

不幸的是,没有解决它,但很好的发现都是一样的:-) – Danny 2012-01-28 17:42:22

0

我确定以下和它的作品对我的XAMPP 1.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 Euros = 3.9792 U.S. dollars

0

我是新来的StackOverflow,所以还不知道什么时候最好使用意见,新的答案..

但深入研究后,与此同时,我发现了一个很简单的PHP货币转产脚本:http://www.expertcore.org/viewtopic.php?f=67&t=2161

这实际上正是我所期待的,非常轻巧,完全符合我的项目需求......也许它也有助于其他人......