codeiginter不断绕过我的ajax块
<script type="text/javascript">
$(document).ready(function() {
$(".btnsubmit").click(function(e){
e.preventDefault();
var name = $("input[name='name']").val();
var email = $("input[name='email']").val();
var form = $(".vol-form");
var url = form.attr('action');
var type = form.attr('method');
var baseurl = "<?php echo base_url(); ?>";
var full_url = baseurl+url;
var formdata = {"name":name, "email":email};
alert(full_url);
$.ajax({
url:full_url,
method:"post",
dataType:"json",
data:formdata,
success: function (response) {
alert(1);
alert(response.success);
},
error: function () {}
});
alert(3);
//return false;
});
});
</script>
除ajax块外,其他所有东西都在运行。在网上进行全面搜索后,我不得不将其发布,但尚未找到解决方案。
这是控制器
public function validate_form()
{
$response['success'] = $this->input->post('name');
echo json_encode($response);
}
这是视图表单的标题
<form name="volunteer_form" method="post" action="welcome/validate_form" class="volunter-form vol-form">
这是.htaccess文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
所以我修改了ajax块
$.ajax({
url:baseurl+'welcome/tryme',
type:'POST',
data:form.serialize(),
dataType:'json'
})
.done(function(response) {
console.log(response.success);
})
.fail(function(response){
console.log(response.error);
});
我已经定义了这条路线
$route['welcome/tryme'] = 'welcome/tryme';
$config['csrf_exclude_uris'] = array('welcome/tryme');
这时,ajax块能够运行,但仍然找不到URL,并返回xhr 404(未找到)错误。但是,单击控制台上的链接可以很好地运行,并且可以看到我的响应,我也将js文件移到了资产。
下面是名为“欢迎”的控制器。
public function tryme()
{
$response['success'] = $this->input->post('name');
$response['error'] = "did not work";
echo json_encode($response);
}
答案 0 :(得分:0)
尝试使用jQuery post方法进行直接调用。更有用。您的示例没有真实的表单数据。
pci.contact_type_code