2012-01-28 14 views
0

私は現在、Jquery、curl、ajax、Google apiを使用して通貨変換スクリプトを実装しようとしていますが、いくつか問題があります。だからここ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); 
      } 
     }); 
    }); 
}); 

そして、ここでは、私はそれが全体を出力している変換ボタンをクリックしたときに

<?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']; 

は今問題があるconversion.phpに持っているものであるのjquery +アヤックスであります$ xではなく#results divのWebページを

私は一日中ここに費やしていますので、どんな助けにも大変感謝しています。

FYI - カールがインストールされ、正しく

+0

「ウェブページ全体を出力する」とはどういう意味ですか?それはconversion.phpファイルの内容ですか?ブラウザーやcurlコマンドラインからurl "conversion.php"(paramsとともに)を試して、正しく動作するかどうか確認することもできます。 – rsmoorthy

+0

申し訳ありませんが、私はスクリプトをテストしているウェブページを参照しています。したがって、#resultには、全体のWebページ(ヘッダー、ナビゲーションメニュー、コンテンツなど)が表示されます。 – Danny

+0

成功関数内でconsole.log(data)を試してみてください。 Firebugのようなブラウザデバッガをチェックインして、送信されたPOST要求とサーバから受信した応答を確認します。それはおそらく助けになるでしょう。 – rsmoorthy

答えて

0

は確かに言うことはできませんが、あなたがevalをこだましているなぜ私は表示されません($アウトを「;」。)?エコーなしでのみevalを呼び出します。

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;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

+0

は、ajaxコールの成功コールバックの発砲ですか?もしそうなら、あなたは何をデータ変数に入れていますか? – Juank

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

0

をロードし、私は次のように固定され、それが有効になってカール拡張子を持つXAMPP 1.7.7に私のために動作します。

すべてのキーのための

$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がフォーム送信ボタンである場合、デフォルト動作を防止します。

amount=3&from=EUR&to=USD戻り 3 Euros = 3.9792 U.S. dollarsを投稿
$("#convert").click(function (event) { 
    event.preventDefault(); 
    var from = $("#from").val(); 
    // ... 
}); 

。私はstackoverflowのために新たなんだ、そう...最高のコメントは、新しい答えを使用する際にまだ

を知っているが、深く研究した後、その間に私がスクリプトを変換する非常に単純なPHPの通貨を発見していない

関連する問題