我只是想问一下如何从我的ajax请求中预览数据?我只是想知道数据是否正确.
这是我的代码.
<script type="text/javascript">
var globalBase_Url = "{$base_url}" + "index.PHP/user_controller/processAdd/";
//alert(globalBase_Url);
{literal}
$(document).ready(function(){
$('#add_cat').on('click',function(){
$('#add_category').show('slide');
});
$('#submit').on('click',function(){
var name = $('#category_name').val();
var desc = $('#description').val();
console.log(globalBase_Url);
$.ajax({
type: 'POST',
url: globalBAse_Url,
data: {cat_name:name,cat_desc:desc}, //How can I preview this?
dataType: 'json',
async: false,
success: function(d){
}
});
});
});
{/literal}
</script>
在我的控制器中我有这个.
public function processAdd(){
$cat_name = $this->input->post('cat_name');
$cat_desc = $this->input->post('cat_desc');
}
我使用了chrome开发人员工具并在XHR中预览它的响应.但我没有看到我的数据.顺便说一下,我正在使用CodeIgniter
解决方法:
在你的控制器中:
你应该有这样的东西
public function processAdd(){
if($this->input->post()){
//do something
$var1 = $this->input->post('cat_name');
$var2 = $this->input->post('cat_desc');
echo json_encode(array('status' => 'ok')); //must be encode with array to access it in your response as an object since you use DataType:json
}
}
然后在你的ajax中,访问响应
$.ajax({
type: 'POST',
url: '/controllers/processAdd',
data: {cat_name:name,cat_desc:desc}, //How can I preview this?
dataType: 'json',
async: false, //This is deprecated in the latest version of jquery must use Now callbacks
success: function(d){
alert(d.status); //will alert ok
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。