[英]Ajax json always returning error although json format in response is correct
I have been trying to implement ajax login with CakePHP 1.3. 我一直在尝试用CakePHP 1.3实现ajax登录。 I have a popup with a simple username/pass login. 我有一个带有简单用户名/密码登录的弹出窗口。
Following is in my views/elements/login.ctp : 以下是我的观点/elements/login.ctp :
echo $this->Form->create('User', array('url'=>array('controller'=>'users','action'=>'login'), 'id'=>'user_login'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login'));
Following is in my controller controllers/users_controller.php 以下是我的控制器中的controllers / users_controller.php
public function ajax_login() {
$response = array('success'=>false);
if($this->RequestHandler->isPost()) {
if($this->Auth->login()) {
$response = array('success'=>"true");
} else {
$response = array('success'=>false);
}
}
$this->set('response', $response);
}
The view for the above controller is under views/users/ajax_login.ctp has ONLY this line: 上述控制器的视图位于views / users / ajax_login.ctp下 ,只有以下这一行:
echo $javascript->object(isset($response) ? $response : array());
My Ajax has the following code: 我的Ajax具有以下代码:
function login_user(){
var username = $("#UserUsername").val();
var password = $("#UserPassword").val();
if(username == "" || username == null || password == "" || password == null){
alert("Please enter a username and password");
return false;
}
$.ajax({
url:"/users/ajax_login",
type:"POST",
data:$('#user_login').serialize(),
dataType:"json",
async: true,
success: function() { console.log("success"); },
error: function(msg) { console.log(msg); }
});
return false;
}
Now, everything seems to be working perfectly, however, this is always failing into the "error" callback and I dont know why. 现在,一切似乎都运行良好,但是,这始终无法进入“错误”回调,而且我不知道为什么。 I've read all of the below links on stackoverflow, and none of them seem to be the issue! 我已经阅读了stackoverflow上的以下所有链接,但似乎都不是问题!
Weird JSON behavior when requesting a json file via $.ajax malformed JSON while JSON is valid? 通过$ .ajax 格式错误的JSON 请求JSON文件 而JSON有效 时,奇怪的JSON行为 ? Ajax error result with struts2 json_encode creating a malformed JSON (with extra hidden character) php json_encode not returning proper json encoded string struts2 json_encode 导致Ajax错误结果,导致 格式 错误的 JSON(带有额外的隐藏字符) php json_encode没有返回正确的json编码字符串
The ONLY thing I suspect is when I read the console.log(msg)
of the error, I get the correct HTML response {"success":true} which is in correct format... BUT... the "responseText" I get something like this: 我唯一怀疑的是,当我读取错误的console.log(msg)
时,会得到正确的HTML响应{“ success”:true} ,其格式正确...但是...“ responseText”得到这样的事情:
responseText: "{"success":true}<!-- 0.375s -->" responseText:“ {”成功“:true} <!-0.375秒->”
so basically im guessing it's this "<!-- 0.375s -->" which is causing the json format to always fail in my ajax call. 所以基本上我猜是因为这个“ <!-0.375s->”导致json格式在我的ajax调用中总是失败。 how on earth do i get rid of this?!... I'm no longer sure if this is a CakePHP issue, or and AJAX/JSON issue!... i've worked on both for over 5 years and now im stuck! 我到底该如何摆脱呢?!...我不再确定这是CakePHP问题还是AJAX / JSON问题!...我已经从事了5年以上的工作,现在我已经卡住!
Indeed the string <!-- 0.375s -->
is breaking your code. 实际上,字符串<!-- 0.375s -->
破坏了您的代码。 Try to find out where it is comming from. 尝试找出它来自哪里。 Some steps: 一些步骤:
debug($this->layout)
in your Controleler to see which on it is. 尝试在Controleler中debug($this->layout)
看看它在哪一个上。 This may very well be in there since I don't see you setting the layout to ajax
for example. 这可能很好,因为我看不到您将布局设置为ajax
。 Cake has this ajax
layout, which essentially is an empty layout. Cake具有这种ajax
布局,本质上是一个空布局。 It should not contain anything more than echo $content_for_layout;
它只应包含echo $content_for_layout;
in CakePHP 1.3. 在CakePHP 1.3中。 echo json_encode($response);
尝试用echo json_encode($response);
替换当前的视图代码echo json_encode($response);
or to keep the trilateral check: echo isset($response) ? json_encode($response) : '';
还是保留三边检查: echo isset($response) ? json_encode($response) : '';
echo isset($response) ? json_encode($response) : '';
The JavaScriptHelper will to the same thing anyway. 无论如何,JavaScriptHelper都会做同样的事情。 Config/core.php
- I am guessing it is 3. 降低Config/core.php
调试值-我猜是3。 I am guessing that checking the layout and setting it to ajax
or empty
will solve your problem. 我猜想检查布局并将其设置为ajax
或为empty
将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.