【问题标题】:Jquery Ajax in HTML page to Codeigniter post data handlingHTML 页面中的 Jquery Ajax 到 Codeigniter 发布数据处理
【发布时间】:2019-01-30 14:14:45
【问题描述】:

我的代码点火器设置之外有一个 HTML 页面。假设 abc.com 是我基于 CI 的 CMS 网站,我将 abc.com/trade 作为我的潜在客户捕获页面。这个 HTML 页面 (index.html) 在 trade 文件夹下。我不能把它放在 CI 中,所以这是一个独立的页面,我的 Apache 正在显示这个 index.html。到目前为止,一切都很好。 现在我在这个 html 页面中有一个联系我们的表单,我正在使用 Jquery ajax 将它发布到控制器。在我的控制器中无法访问数据,如果我返回发布的值,则会在成功方法(Jquery)中出现一个空白数组。

我尝试使用 Json.stringify 发布它,但我没有得到它。

HTML 代码

<form method="post" action="">
<div class="form-group">
<input name="f_full_name" type="text" placeholder="Name" required>
</div>
<div class="form-inline">
<div class="form-group">
<input name="f_contact_number" type="text" placeholder="Number+" value="" required>
</div>
<div class="form-group">
<input name="f_email_address" type="email" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<textarea name="f_description" placeholder="Description"></textarea>
</div>
<input type="hidden" name="form2">
<button id="footer_enquiry" name="footer_enquiry" value="Submit">Submit</button>
</form>

JQUERY

$("#footer_enquiry").click(function(e){
e.preventDefault();
var ajxUrl      = "https://www.example.com/contactus/enquiry";
var ajxDataObj  = {
'name'              : $('input[name=f_full_name]').val(),
'contact'           : $('input[name=f_contact_number]').val(),
'email'         : $('input[name=f_email_address]').val(),
'description'       : $('[name=f_description]').val()
};
var ajxData     = JSON.stringify(ajxDataObj);
console.log(ajxData);
$.ajax({
type:         "POST",
url:          ajxUrl,
data:         ajxData,
contentType:  "json",
success: function (result) {
//do somthing here
alert(result);
console.log(result);
return false;
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}   
alert(msg);
}                                                                                   
}); 

控制器中的 PHP 代码 函数查询() {

    $name               = $this->input->post('name');
    $contact            = $this->input->post('contact');
    $email              = $this->input->post('email');
    $description        = $this->input->post('description');

    $data=array(
        'name'          => $name,
        'contact'       => $contact,
        'email'         => $email,
        'description'   => $description
    );

    print_r($data);

}

控制台输出我收到的内容

In
{"name":"test","contact":"01234567890","email":"gireesh.t@floretmedia.org","description":"testestestesetsetsetset"}
Array
(
[name] => 
[contact] => 
[email] => 
[description] => 
)

我希望发布的数据会收到警报,但即将出现空白数组

【问题讨论】:

  • 你试过不是contentType,而是“dataType:'json'”吗?尝试使用“cache:false”,可能发布的数据是错误的。

标签: php jquery html codeigniter


【解决方案1】:

看看这个 QA Handling JSON data in Codeigniter Controller,您的后端可能正在接收数据,但 $this-&gt;input-&gt;post 正在寻找正在发布的表单数据,而不是 JSON。

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 2012-03-02
    • 2017-01-21
    • 1970-01-01
    • 2011-05-13
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多