从PHP文件到PHP文件的Ajax调用

时间:2015-07-30 16:37:35

标签: php jquery ajax

这是我在php文件中执行的ajax调用:

$.ajax({
                type: "POST",
                url: "../includes/filters.php",
                data: {"email" : email},
                dataType: "json",
                success: function(data) 
                {
                    if(data.equals('sent'))
                     {  
                        alert(data);
                     }
                    else
                    {
                        alert("There Was Some Problem Please Try Again!");
                    }

                },
                    error: function(xhr, status, text) {
                        console.log(xhr);
                        console.log(status);
                        console.log(text);
                    }
              });

这里是filters.php文件:

$email = $_POST['email'];
        $email = filter_var($email, FILTER_VALIDATE_EMAIL);
        $query = "---------some query------";
        $stmt = $mysqli->prepare($query);
        $stmt->bind_param("s", $email);                 
        $stmt->execute();
        $stmt->store_result();
        $temp = $stmt->num_rows;
        if ($temp == 1)         
        return json_encode('true');
        else 
        return json_encode('false');

这是我在consol中遇到的错误

Object {readyState: 4, responseText: "", status: 200, statusText: "OK"}
(index):120 parsererror
(index):121 SyntaxError: Unexpected end of input
    at Object.parse (native)
    at n.parseJSON (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:5497)
    at ub (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:7521)
    at x (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:10935)
    at XMLHttpRequest.n.ajaxTransport.k.cors.a.crossDomain.send.b (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:14765)

请帮帮我,因为我已经尝试了所有的JSON转换,我甚至尝试转换一个带有字符串的php变量,没有导致

3 个答案:

答案 0 :(得分:1)

您需要echo JSON响应,而不是return

echo json_encode($temp == 1 ? 'true' : 'false');

此外,您的Javascript代码要求响应成功时为sent,而不是truefalse

在Javascript中,您将事物与==进行比较,而不是.equals()

if (data == "sent")

答案 1 :(得分:0)

filters.php没有输出;您需要echo而不是return

不使用JSON:

    $email = $_POST['email'];
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    $query = "---------some query------";
    $stmt = $mysqli->prepare($query);  // XXX $mysqli is not initialized
    $stmt->bind_param("s", $email);                 
    $stmt->execute();
    $stmt->store_result();
    echo $stmt->num_rows == 1 ? "sent" : "not-sent";

答案 2 :(得分:0)

我很确定您的json_encode('true');的结果实际上并不构成您在ajax调用中指定的有效JSON,因为您期望将结果返回为dataType,这就是你得到解析错误的原因。

echo json_encode('true');

给出

"true"

在任何想象中都不是json弦。

试试这个

$result = array();
$result['status'] = 'not sent';

if ($temp == 1) {
    $result['status'] = 'sent';
}

echo json_encode($result);

现在在你的javascript成功函数

success: function(data) 
{
    if(data.status == 'sent') {  
        alert(data.status);
    } else {
        alert("There Was Some Problem Please Try Again!");
    }

},