Codeigniter 中的 JQuery Ajax POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17590688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Ajax POST in Codeigniter
提问by Dennis
I have searched a lot of tutorials with POST methods and saw answered questions here too but my POST still doesn't work...I thought i should post it here if you guys see something that i don't!
我用 POST 方法搜索了很多教程,也在这里看到了回答的问题,但我的 POST 仍然不起作用……我想如果你们看到我没有看到的东西,我应该把它贴在这里!
My js - messages.js:
我的 js - messages.js:
$(document).ready(function(){
$("#send").click(function()
{
$.ajax({
type: "POST",
url: base_url + "chat/post_action",
data: {textbox: $("#textbox").val()},
dataType: "text",
cache:false,
success:
function(data){
alert(data); //as a debugging message.
}
return false;
});
});
My view - chat.php:
我的观点 - chat.php:
<?php $this->load->js(base_url().'themes/chat/js/messages.js');?> //i use mainframe framework which loading script this way is valid
<form method="post">
<input id="textbox" type="text" name="textbox">
<input id="send" type="submit" name="send" value="Send">
</form>
Last My controller - chat.php
最后我的控制器 - chat.php
//more functions here
function post_action()
{
if($_POST['textbox'] == "")
{
$message = "You can't send empty text";
}
else
{
$message = $_POST['textbox'];
}
echo $message;
}
回答by Ganesh RJ
$(document).ready(function(){
$("#send").click(function()
{
$.ajax({
type: "POST",
url: base_url + "chat/post_action",
data: {textbox: $("#textbox").val()},
dataType: "text",
cache:false,
success:
function(data){
alert(data); //as a debugging message.
}
});// you have missed this bracket
return false;
});
});
回答by Adnan Ahmad
In codeigniter there is no need to sennd "data" in ajax post method.. here is the example .
在 codeigniter 中,不需要在 ajax post 方法中发送“数据”.. 这是示例。
searchThis = 'This text will be search';
$.ajax({
type: "POST",
url: "<?php echo site_url();?>/software/search/"+searchThis,
dataType: "html",
success:function(data){
alert(data);
},
});
Note : in url , software is the controller name , search is the function name and searchThis is the variable that i m sending.
注意:在 url 中,software 是控制器名称,search 是函数名称,searchThis 是我发送的变量。
Here is the controller.
这是控制器。
public function search(){
$search = $this->uri->segment(3);
echo '<p>'.$search.'</p>';
}
I hope you can get idea for your work .
我希望你能对你的工作有所了解。
回答by Vicky
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class UserController extends CI_Controller {
public function verifyUser() {
$userName = $_POST['userName'];
$userPassword = $_POST['userPassword'];
$status = array("STATUS"=>"false");
if($userName=='admin' && $userPassword=='admin'){
$status = array("STATUS"=>"true");
}
echo json_encode ($status) ;
}
}
function makeAjaxCall(){
$.ajax({
type: "post",
url: "http://localhost/CodeIgnitorTutorial/index.php/usercontroller/verifyUser",
cache: false,
data: $('#userForm').serialize(),
success: function(json){
try{
var obj = jQuery.parseJSON(json);
alert( obj['STATUS']);
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
}
回答by Pattle
The question has already been answered but I thought I would also let you know that rather than using the native PHP $_POST I reccomend you use the CodeIgniter input classso your controller code would be
这个问题已经得到了回答,但我想我也会让你知道,我建议你使用CodeIgniter 输入类而不是使用本机 PHP $_POST ,这样你的控制器代码就会是
function post_action()
{
if($this->input->post('textbox') == "")
{
$message = "You can't send empty text";
}
else
{
$message = $this->input->post('textbox');
}
echo $message;
}
回答by Suraj R Gholap
<script>
$("#editTest23").click(function () {
var test_date = $(this).data('id');
// alert(status_id);
$.ajax({
type: "POST",
url: base_url+"Doctor/getTestData",
data: {
test_data: test_date,
},
dataType: "text",
success: function (data) {
$('#prepend_here_test1').html(data);
}
});
// you have missed this bracket
return false;
});
</script>