0

从域返回 json 时遇到问题。

在一个文件中,我有以下内容

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中。

另请阅读header json https://stackoverflow.com/questions/267546/correct-http-header-for-json-file

于 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 回答