0) { console.log("inpu-6ren">
gpt4 book ai didi

javascript - JQuery ajax 发布到 PHP 服务器无法正常工作 - "SyntaxError: Unexpected token < in JSON at position 0"

转载 作者:行者123 更新时间:2023-12-01 03:11:34 25 4
gpt4 key购买 nike

我正在尝试将json数据发送到服务器,并接收json响应,代码如下:

JS:

function login() {

console.log("clicked");

//get values of form into variables
var email = $("#email").val();
var password = $("#password").val();

//create data array
var dataString = {"email": email, "password": password};

console.log(dataString);

//check for blank inputs
if ($.trim(email).length == 0) {
myApp.alert("Email required", "Login Failed");
} else if ($.trim(password).length == 0) {
myApp.alert("Password required", "Login Failed");
}

//if form isnt empty, post ajax request to server
if ($.trim(email).length > 0 & $.trim(password).length > 0) {

console.log("input checked");

$.support.cors = true;

//ajax post
$.ajax({
type: "POST",
url: "http://gingr-server.com/login.php",
data: dataString,
dataType: 'json',
crossDomain: true,
cache: false,

beforeSend: function() {
$("#login").val('Connecting...');
console.log("connecting to server");
},

//display success/fail message - put something in data on server
success: function(data, textString, xhr) {
if (data.status == "correct") {
localStorage.login = "true";
localStorage.email = email;
localStorage.id = data.id;
mainView.router.loadPage("swipe.html");
} else {
myApp.alert("Incorrect email or password", "Login Failed");
}
},

error: function(xhr, ajaxOptions, errorThrown) {
console.log(xhr);
console.log(errorThrown);
myApp.alert("Unknown error, please try again", "Login Failed");
},

complete: function(data) {
if (data.readyState == "0") {
console.log("unsent");
} else if (data.readyState == "4") {
console.log("done");
}
}

});
}
return false;
}

PHP:

<?php
header("access-control-allow-origin: *");
header("access-control-allow-methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header("access-control-allow-headers: X-Requested-With, Content-Type");

header('Content-Type: application/json');

//include databse info
require_once "db_config.php";

$email = $mysqli->real_escape_string(htmlspecialchars($_POST["email"]));
$password = $mysqli->real_escape_string(htmlspecialchars($_POST["password"]));

//get hashed password from db
$result = $mysqli->query("SELECT password, id FROM user_table WHERE
email = '$email'");
$row = $result->fetch_assoc();
$passwordHash = $row["password"];
$userID = $row["id"];

//free memory
$result->free();

//verify password for that email
if (password_verify($password, $passwordHash)) {
$response = array( "status" => "correct",
"id" => $userID));
} else {
$response = array("status" => "incorrect"));
}

echo json_encode($response);

$mysqli->close();
?>

错误信息如下:

语法错误:JSON 中位置 0 处出现意外标记 < 在解析()

对我来说,客户端似乎正在接收 HTML(因此是 < 标签)而不是 json,但我不知道为什么!

最佳答案

找到了!一点也不聪明...

$response = array(  "status" => "correct",
"id" => $userID));

发现));造成所有问题...我认为就寝时间

关于javascript - JQuery ajax 发布到 PHP 服务器无法正常工作 - "SyntaxError: Unexpected token < in JSON at position 0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45805757/

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