0

ドメインからjsonを返すときに問題が発生します。

1つのファイルに私は次のものを持っています

var postData ="domain=testing.gr";

$.ajax({
    type: "POST",
    dataType: "json",
    data: postData,
    beforeSend: function(x) {
        if(x && x.overrideMimeType) {
            x.overrideMimeType("application/json;charset=UTF-8");
        }
    },
    url: 'http://www.ewebs.gr/advprodigy/adv.php',
    success: function(data) {
        // 'data' is a JSON object which we can access directly.
        // Evaluate the data.success member and do something appropriate...
        if (data.success == true){
            $('#keimeno').html(data.message);
        }
        else{
            $('#keimeno').html(data.message);
        }
    }
});

...そしてPHPファイルで私は以下をハードコーディングしました:

<?php header('content-type: application/json');

    $domain = $_POST['domain'];

    // Set up associative array
    $data = array('success'=> true,'message'=>'Success message: hooray!');

    // JSON encode and send back to the server
    echo json_encode($data);

?>

それでも、戻るとエラーが発生します。応答は空です。 http://prntscr.com/x333k

4

3 に答える 3

0

jsonここであなたはあなたから得られなかったserverのでこれを試してください、

<?php 
    //header('content-type: application/json');

    //$domain = $_POST['domain'];

    // Set up associative array
    $data = array('success'=> true,'message'=>'Success message: hooray!');

    // JSON encode and send back to the server
    echo json_encode($data);
?>

これが機能する場合は、これerror_reporting(-1);をに追加しphp fileて取得しますerror

https://stackoverflow.com/questions/267546/correct-http-header-for-json-fileについてもこれを読んでくださいheader json

于 2013-03-20T15:50:04.933 に答える
0

これをサーバーに置いてみてください:

header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json'); 
于 2013-03-20T16:03:15.177 に答える
-1

使ってみて

{
type: "POST",
contentType : "application/json; charset=utf-8",
dataType : "json",...}
于 2013-03-20T15:48:29.407 に答える