代码点火-如何从控制器返回Json响应
16lz
2021-01-22
How do I return response from the controller back to the Jquery Javascript?
如何从控制器返回到Jquery Javascript的响应?
Javascript
Javascript
$('.signinform').submit(function() {
$(this).ajaxSubmit({
type : "POST",
url: 'index.php/user/signin', // target element(s) to be updated with server response
cache : false,
success : onSuccessRegistered,
error: onFailRegistered
});
return false;
});
Data is returned null (blank)!
返回的数据为空(空白)!
function onSuccessRegistered(data){
alert(data);
};
Controller -
控制器- - - - - -
public function signin() {
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode( $arr );
}
3 个解决方案
#1
32
//do the edit in your javascript
$('.signinform').submit(function() {
$(this).ajaxSubmit({
type : "POST",
//set the data type
dataType:'json',
url: 'index.php/user/signin', // target element(s) to be updated with server response
cache : false,
//check this in Firefox browser
success : function(response){ console.log(response); alert(response)},
error: onFailRegistered
});
return false;
});
//controller function
public function signin() {
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
//add the header here
header('Content-Type: application/json');
echo json_encode( $arr );
}
更多相关文章
- 角度服务和控制器共同范围
- AngularJS(1.5.8) - 如何直接从获取json对象的控制器中填充选择选
- 如何在rails中使用ajax将数据从控制器传递到视图
- Angular使用jQuery插件与外部控制器中的对象
- jQuery Ajax未能调用MVC 4控制器方法。
- 从json获取最大插槽并应用于控制器
- 使用片段活动创建的ADT空白活动。
- 问题记录-Activity跳转后显示空白界面