2

jqueryを使用してクロスドメインリクエストを作成しようとしました。これは、クライアント側がどのように見えるかです。

<script src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready( function(){
    $.ajax({
        type: 'GET',
        url: "http://www.xserver.com/xdomainhandler.php",
        processData: true,
        data: {
            type:"gotohell"
        },
        dataType: "json",
        success: function (data) {
            myglob=data;
            var repo=JSON.parse(myglob);
            alert(repo.type);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown){
            alert('ends up in error state');

        }
    });
});
</script>  

このリクエストを受け取るサーバー ページのコードは次のようになります。

<?php
header('Access-Control-Allow-Origin: *');  
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
session_cache_limiter('nocache');

$arr = array ('response'=>'success','comment'=>'test comment here','type'=>$_GET['type']);
echo json_encode($arr);
?>

しかし、リクエスト/レスポンス プロセスを完了したとき、「repo」変数には何も入っていませんでした。firebug を使用して応答を確認したところ、次のような応答が表示されます。

{"response":"success","comment":"test comment here","type":"gotohell"}

また、表示される firebugの DOM パネルで myglob変数を確認しました。

Object { response="success", comment="test comment here", type="gotohell"}

しかし、myglob をレポに解析すると、何も表示されません。どこが間違っているのか。誰か助けてくれませんか。ありがとう!

4

2 に答える 2

3

You don't need to parse it as jQuery parses it for yo so avoid

        var repo=JSON.parse(myglob);

and just call

  alert(data.type);
于 2012-04-12T10:41:55.740 に答える
2

Because you supplied dataType: 'json' jQuery will already have parsed the response - parsing it again will cause errors. Remove the following line:

var repo = JSON.parse(myglob);
于 2012-04-12T10:42:10.987 に答える