如何解决数据没有从ajax传递到控制器
我正在尝试进行谷歌身份验证。在 add.PHP(templates file) 我写了这个 javascript 代码。
function singIn() {//<------this is called when button is pressed
firebase.auth()
.signInWithPopup(provider)
.then((result) => {
console.log(result);
var credential = result.credential;
// This gives you a Google Access Token. You can use it to access the Google Api.
var token = credential.accesstoken;
// The signed-in user info.
var user = result.user;
var userInfo = JSON.stringify(user); //to Post
$.ajax({
method: “POST”,url: “<?= Router::url(array('controller' => 'Users','action' => 'googlesignin')); ?>”,data: {
userInfo
},success: function(data) {
console.log(“Signed in google!”);
},error: function(param) {
console.log("Something went wrong" + param.responseText.message);
console.log("-----------------------");
console.log(param);
}
});
我收到了 google pop,一切正常,没有任何错误,但数据没有传递到控制器。我的控制器
public function googlesignin(){
echo $_POST['data']['email'];
die;
}
我也加了这个
$builder->connect(’/’,array(‘controller’ => ‘Users’,‘action’ => ‘googlesignin’));
在 routes.PHP 中。
谁能告诉我问题出在哪里?
解决方法
在阿贾克斯
$.ajax({
// ...
headers: {
"Accept":"application/json"
}
//...
在你的控制器中
public function googlesignin(){
$post = $this->request->getData(); // All post data available here
// pr($post); // print all data
echo $post['data']['email'];
die;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。