0

验证.php

<?php
header('Content-type: application/json');
include 'config.php';
$con = mysqli_connect($db_host, $db_user, $db_pwd, $database);
$key1 = $_GET['key1'];
$name = $_GET['name'];
$email = $_GET['email'];
$phone = $_GET['phone'];
$address = $_GET['address'];
$installations = mysql_query("SELECT * FROM installations where Key1 = '$key1'");
if ($installations !== FALSE && mysql_num_rows($installations) > 0) {
    while ($row = mysql_fetch_array($installations)) {
        $key2 = $row['Key2'];
        $status = $row['Status'];
        if ($status == 0) {
            $sql = "UPDATE installations SET Name = '$name', Email = '$email', Address = '$address', Phone = '$phone',Status = '1' WHERE Key1 = '$key1'";
            mysqli_query($con, $sql);
            $data = array('key1' => $key1, 'key2' => $key2);
            echo(json_encode($data));
        } else {
            $data = array('key1' => 'key not valid', 'key2' => 'key not valid');
            echo(json_encode($data));
        }
    }
} else {
    $data = array('key1' => 'key not valid', 'key2' => 'key not valid');
    echo(json_encode($data));
}
?>

我的脚本

jQuery.ajax({
    type : "GET",
    url : "http://example.com/verify.php",
    data : {
        name : name,
        email : email,
        address : address,
        phone : phone,
        key1 : key1
    },
    cache : false,
    success : function(data) {
        jQuery.ajax({
            type : "GET",
            url : "admin/check_verification.php",
            data : {
                key1 : data.key1,
                key2 : data.key2
            },
            cache : false,
            success : function(response) {
                $('.message_outer').fadeIn('slow').html(response);
                window.setTimeout(function() {
                    location.reload();
                }, 2000);
            }
        });
    },
    error : function(jqXHR, exception) {
        if (jqXHR.status === 0) {
            alert('Not connect.\n Verify Network.');
        } else if (jqXHR.status == 404) {
            alert('Requested page not found. [404]');
        } else if (jqXHR.status == 500) {
            alert('Internal Server Error [500].');
        } else if (exception === 'parsererror') {
            alert('Requested JSON parse failed.');
        } else if (exception === 'timeout') {
            alert('Time out error.');
        } else if (exception === 'abort') {
            alert('Ajax request aborted.');
        } else {
            alert('Uncaught Error.\n' + jqXHR.responseText);
        }
    }
});

我收到“未连接,验证网络”错误。但是数据(姓名,电子邮件,地址,电话,key1)保存在我的服务器数据库中。我已使用本地主机成功测试了此代码。我该如何解决这个错误。请帮我。

4

1 回答 1

0

我修复了这个错误,添加header("Access-Control-Allow-Origin: *"); 到我的服务器 verify.php 。感谢@SLaks。来自这里的代码http://enable-cors.org/server_php.html

<?php
header("Access-Control-Allow-Origin: *");
header('Content-type: application/json');
include 'config.php';
$con = mysqli_connect($db_host, $db_user, $db_pwd, $database);
$key1 = $_GET['key1'];
$name = $_GET['name'];
$email = $_GET['email'];
$phone = $_GET['phone'];
$address = $_GET['address'];
$installations = mysql_query("SELECT * FROM installations where Key1 = '$key1'");
if ($installations !== FALSE && mysql_num_rows($installations) > 0) {
    while ($row = mysql_fetch_array($installations)) {
        $key2 = $row['Key2'];
        $status = $row['Status'];
        if ($status == 0) {
            $sql = "UPDATE installations SET Name = '$name', Email = '$email', Address = '$address', Phone = '$phone',Status = '1' WHERE Key1 = '$key1'";
            mysqli_query($con, $sql);
            $data = array('key1' => $key1, 'key2' => $key2);
            echo(json_encode($data));
        } else {
            $data = array('key1' => 'key not valid', 'key2' => 'key not valid');
            echo(json_encode($data));
        }
    }
} else {
    $data = array('key1' => 'key not valid', 'key2' => 'key not valid');
    echo(json_encode($data));
}
?>
于 2013-09-17T06:45:55.730 回答