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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口