这是我的register.js
var formdata = new FormData();
formdata.append("name", name.value);
formdata.append("username", username.value);
formdata.append("email", email.value);
formdata.append("password", password.value);
var ajax = new XMLHttpRequest();
var response = eval(ajax.responseText);
alert(response);
ajax.open("POST", "/req/register/register.req.php");
ajax.send(formdata);
并且有我的register.req.php
require '../../inc/global.inc.php';
ini_set('display_errors', 0);
list($first_name, $last_name) = explode(" ", $_POST['name']);
$db->query("INSERT INTO users (first_name, last_name, username, password, email) VALUES ('%s', '%s', '%s', '%s', '%s')", $first_name, $last_name, $_POST['username'], md5($_POST['password']), $_POST['email']);
header('Content-Type: application/json');
echo json_encode(array('OK'));
问题是如何获得' OK' string,因为var response = eval(ajax.responseText);得到一个未完成的'变量
答案 0 :(得分:2)
您可以在onreadystatechange方法中获得响应。
var ajax = new XMLHttpRequest();
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && xmlhttp.status==200)
{
var response = ajax.responseText;
alert(response);
}
}
ajax.open("POST", "/req/register/register.req.php");
ajax.send(formdata);