I just want to ask how can I preview the data from my ajax request? I just want to know if the data is correct.
我只是想问一下如何从我的ajax请求中预览数据?我只是想知道数据是否正确。
Here's my code.
这是我的代码。
<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>
In my controller I have this.
在我的控制器中,我有这个。
public function processAdd(){
$cat_name = $this->input->post('cat_name');
$cat_desc = $this->input->post('cat_desc');
}
I used the chrome developer tools and preview it's response in XHR. But I don't see my data. By the way I am using CodeIgniter
我使用了chrome开发人员工具并在XHR中预览了它的响应。但我没有看到我的数据。顺便说一句,我正在使用CodeIgniter
3 个解决方案
#1
6
in your controller:
在你的控制器中:
you should have something like this
你应该有这样的东西
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
}
}
then in your ajax, to access the response
然后在你的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
}
});
#2
2
You can use the browser developer tools > NetWork
您可以使用浏览器开发人员工具> NetWork
Chrome: Developer Tools
FireFox: Developer Tools - Network/Fire Bug -> Network
Chrome:开发者工具FireFox:开发者工具 - 网络/火虫 - >网络
or
in the ajax success handler, log the data to the console
在ajax成功处理程序中,将数据记录到控制台
success: function(d){
console.log(d);// then look at the developer tools -> console
}
#3
1
try putting
var globalBase_Url = "{$base_url}" + "index.php/user_controller/processAdd/";
inside {literal} tag
在{literal}标签内
#1
6
in your controller:
在你的控制器中:
you should have something like this
你应该有这样的东西
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
}
}
then in your ajax, to access the response
然后在你的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
}
});
#2
2
You can use the browser developer tools > NetWork
您可以使用浏览器开发人员工具> NetWork
Chrome: Developer Tools
FireFox: Developer Tools - Network/Fire Bug -> Network
Chrome:开发者工具FireFox:开发者工具 - 网络/火虫 - >网络
or
in the ajax success handler, log the data to the console
在ajax成功处理程序中,将数据记录到控制台
success: function(d){
console.log(d);// then look at the developer tools -> console
}
#3
1
try putting
var globalBase_Url = "{$base_url}" + "index.php/user_controller/processAdd/";
inside {literal} tag
在{literal}标签内