dongque20030402 2014-12-11 10:14
浏览 36
已采纳

$ .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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真