dongque20030402
2014-12-11 10:14
浏览 35

$ .ajax数据类型:json抛出错误

I'm trying to retrieve a json object through a ajax request from a php file. My ajax request looks like the following:

function validateForm() {
var name = $('#usernameLogIn').val();
var password = $('#passwordLogIn').val();
  $.ajax({
    type: 'GET',
    url: '../webroot/login/validateForm/',
    data: {name: name, password: password},
    dataType: 'json',
    success: function(result) {
    var data = JSON.stringify(result);
    var b = $.parseJSON(data);
    alert(b);

        },
        error: function(a,b,c) { console.log(a,b,c); }
  });
}

and my php file looks like this:

$form = $this->form; 
      $status = false;
      $name = preg_replace("/[^A-Za-z0-9]/", " ", $_GET['name']);
      $formPassword = preg_replace("/[^A-Za-z0-9]/", " ", $_GET['password']);
      $now = date(DATE_RFC2822); 
      $user =  $this->user->findName($name); 

      if(isset($user->name))
      {
        $password = $user->password;
        $status = password_verify($formPassword, $password);
      }

      if ($status === true) 
      { 
         $this->session->set('loggedIn', $this->user->name);
      } 

      else if ($status === false) { 

          $this->session->clearSession('loggedIn');
    }

      $sessionLog = $this->session->get('loggedIn');

      $advert = array(
    'session' => $sessionLog,
    'name' => $name,
    'password' => $formPassword,
 );

echo json_encode($advert);
exit;

Finally when it passes the values back to my Ajax request it goes straight into the error function and prints the following into the console:

Object "parsererror" SyntaxError
message: Unexpected Token <"

Is it any way to tell where this goes wrong and why?

thankfull for answers, cheers.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhongju8780 2014-12-11 10:39
    已采纳

    I think you have verbose set to true in you config file.

    点赞 评论

相关推荐 更多相似问题