gpt4 book ai didi

php - Jquery Ajax获取外部url错误

转载 作者:行者123 更新时间:2023-11-30 01:18:15 27 4
gpt4 key购买 nike

验证.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)保存在我的服务器数据库中。我已使用本地主机成功测试了此代码。我该如何修复这个错误。请帮助我。

最佳答案

我修复了此错误,添加了 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));
}
?>

关于php - Jquery Ajax获取外部url错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18832088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com