admin管理员组

文章数量:1419624

I get

Uncaught SyntaxError: Unexpected token < x.extend.parseJSON jQuery.js:4
$.ajax.success Login.js:15
c jQuery.js:4
p.fireWith jQuery.js:4
k jQuery.js:6
r

The jQuery is a fresh download and the error with my login js is on this line

var json = jQuery.parseJSON(data);

edit here is the login php page, if you need the javascript just tell me. Also would you say this is more effective than having a standard form and page set up:

<?php

require("./../config.php");

if (!isset($_POST['req']))
{
    die("No request provided.");
}
else 
{ 
    if ($_POST['req'] == "login")
    {
        $json = array("success"=>false, "msg"=>"", "req"=>"login");

        if (!isset($_POST['user']) || empty($_POST['user']))
        {
            $json['success'] = false;
            $json['msg'] = "No user provided";
        }
        else
        { 
            if (!isset($_POST['pass']) || empty($_POST['pass']))
            {
                $json['success'] = false;
                $json['msg'] = "No password provided";
            }
            else
            { 
                $user = mysql_real_escape_string($_POST['user']);
                $password = mysql_real_escape_string($_POST['pass']);

                $password = hash("sha512", $password);

                $query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '" . $user . "' AND `password` = '" . mysql_real_escape_string($password) . "' LIMIT 1");

                if (mysql_num_rows($query)) 
                {
                    $sessID = mysql_real_escape_string(session_id());
                    $hash = mysql_real_escape_string(hash("sha512", $sessID.$_SERVER['HTTP_USER_AGENT']));

                    $userData = mysql_fetch_assoc($query);
                    $expires = time() + (60 * 15);

                    mysql_query("INSERT INTO `active_users` (`user`, `session_id`, `hash`, `expires`) VALUES (" . (int) $userData['id'] . ", '" . $sessID . "', '" . $hash . "', " . $expires . ")");
                    $json['success'] = true;
                    $json['msg'] = "Logged in";
                }
                else
                {
                    $json['success'] = false;
                    $json['msg'] = "Username or password are incorrect.";
                }
            }
        }

        print(json_encode($json));
    }
}
?>

I get

Uncaught SyntaxError: Unexpected token < x.extend.parseJSON jQuery.js:4
$.ajax.success Login.js:15
c jQuery.js:4
p.fireWith jQuery.js:4
k jQuery.js:6
r

The jQuery is a fresh download and the error with my login js is on this line

var json = jQuery.parseJSON(data);

edit here is the login php page, if you need the javascript just tell me. Also would you say this is more effective than having a standard form and page set up:

<?php

require("./../config.php");

if (!isset($_POST['req']))
{
    die("No request provided.");
}
else 
{ 
    if ($_POST['req'] == "login")
    {
        $json = array("success"=>false, "msg"=>"", "req"=>"login");

        if (!isset($_POST['user']) || empty($_POST['user']))
        {
            $json['success'] = false;
            $json['msg'] = "No user provided";
        }
        else
        { 
            if (!isset($_POST['pass']) || empty($_POST['pass']))
            {
                $json['success'] = false;
                $json['msg'] = "No password provided";
            }
            else
            { 
                $user = mysql_real_escape_string($_POST['user']);
                $password = mysql_real_escape_string($_POST['pass']);

                $password = hash("sha512", $password);

                $query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '" . $user . "' AND `password` = '" . mysql_real_escape_string($password) . "' LIMIT 1");

                if (mysql_num_rows($query)) 
                {
                    $sessID = mysql_real_escape_string(session_id());
                    $hash = mysql_real_escape_string(hash("sha512", $sessID.$_SERVER['HTTP_USER_AGENT']));

                    $userData = mysql_fetch_assoc($query);
                    $expires = time() + (60 * 15);

                    mysql_query("INSERT INTO `active_users` (`user`, `session_id`, `hash`, `expires`) VALUES (" . (int) $userData['id'] . ", '" . $sessID . "', '" . $hash . "', " . $expires . ")");
                    $json['success'] = true;
                    $json['msg'] = "Logged in";
                }
                else
                {
                    $json['success'] = false;
                    $json['msg'] = "Username or password are incorrect.";
                }
            }
        }

        print(json_encode($json));
    }
}
?>
Share Improve this question edited Jun 18, 2013 at 20:06 user2498606 asked Jun 18, 2013 at 19:59 user2498606user2498606 212 silver badges3 bronze badges 9
  • 2 And the data being parsed looks like...? – j08691 Commented Jun 18, 2013 at 20:00
  • Post your code along with the JSON data here. – Harsha Venkataramu Commented Jun 18, 2013 at 20:00
  • Clearly data isn't valid JSON. What does console.log(data); show? – gen_Eric Commented Jun 18, 2013 at 20:03
  • 1 It sounds like data might be some HTML-based error page sent from the web server instead of the json resource you really wanted. – Chris Farmer Commented Jun 18, 2013 at 20:04
  • As other's have said, post the data you are getting back. I have seen this type of error because the server is returning a 404 or 500 error page and the ajax call is trying to parse the HTML ing back when it is expecting JSON to e back. – John Koerner Commented Jun 18, 2013 at 20:05
 |  Show 4 more ments

1 Answer 1

Reset to default 4

From jQuery 1.9 onwards, the $.parseJSON will throw a JavaScript error rather than returning null. So your json isn't valid to show that error.

From jQuery Docs:

"Prior to jQuery 1.9, $.parseJSON returned null instead of throwing an error if it was passed an empty string, null, or undefined, even though those are not valid JSON."

To shield your JS from throwing error, do the following:

try {
    var json = $.parseJSON(data);
    //if code below here runs, means json is valid


} catch(e){
    //if code below here runs, means json is invalid

}

Ps.: Post your JSON so we can tell what is the problem. You can do by adding console.log(data); before the try (if you use my code).

本文标签: javascriptUncaught SyntaxError Unexpected token ltStack Overflow