我正在使用AJAX发布数据,并希望在成功后将用户重定向到https://example.com/dashboard/ExportExcelSales
。
我曾尝试使用location.href
,但是它没有按预期工作。
下面是我的代码段,使用AJAX运行HTTP POST;
$("#analysis").click(function() {
$.ajax({
type: "POST",
url: "dashboard/ExportExcelSales",
data: {
dateStart: $('#dateStartTanggalbarang').val(),
dateEnd: $('#dateEndTanggalbarang').val(),
area: $('#areaFill').val(),
booth: $('#boothFill').val(),
acam: $('#acamFill').val(),
},
dataType: "JSON",
async: false,
success: function (response) {
location.href = "dashboard/ExportExcelSales";
}
});
});
这是我的控制者;
public function ExportExcelSales() {
$dateawal = $this->input->post("dateStart");
$dateakhir = $this->input->post("dateEnd");
$area = $this->input->post('area');
$booth = $this->input->post('booth');
$acam = $this->input->post('acam');
$data = array(
'data' => $this->M_Sales->tableExport(
$dateawal,
$dateakhir,
$area,
$booth,
$acam
),
'dateakhir' => $dateakhir,
'dateawal' => $dateawal,
'area' => $this->M_Sales->areaSelect($area),
'booth' => $this->M_Sales->boothSelect($booth),
'acam' => $this->M_Sales->acamSelect($acam),
);
$path = "";
$data = array(
"page" => $this->load("Export Data Sales", $path),
"content" =>$this->load->view('layouts/exportExcelSales', $data, true)
);
$this->load->view('layouts/exportExcelSales', $data);
}
我在做什么错,我该如何解决?
答案 0 :(得分:0)
您在后端使用post
,而只是在前端重定向。
您的选择是:
在您的js上,成功时构造URL。喜欢:
$("#analysis").click(function() {
$.ajax({
....
success: function(response) {
var dateStart = $('#dateStart').val(); //Getting values from inputs.
var dateEnd = $('#dateEnd').val(); //Update the input ids accordingly
var area = $('#area').val();
var booth = $('#booth').val();
var acam = $('#acam').val();
location.href = "dashboard/ExportExcelSales?dateStart=" + dateStart + "&dateEnd=" + dateEnd + "&area=" + area + "&booth=" + booth + "&acam=" + acam;
}
});
});
在后端,您可以使用$this->input->get
来获取值
public function ExportExcelSales(){
$dateawal = $this->input->get("dateStart");
$dateakhir = $this->input->get("dateEnd");
$area = $this->input->get('area');
$booth = $this->input->get('booth');
$acam = $this->input->get('acam');
....
}
您可以将您的输入格式如下:
<form id="exportExcelSales" action="layouts/exportExcelSales" method="post">
<input type="text" name="dateStart"><br>
<input type="text" name="dateEnd"><br>
<input type="text" name="area"><br>
<input type="text" name="booth"><br>
<input type="text" name="acam"><br>
</form>
在您的js上,您可以通过调用submit()
方法来提交表单:
$("#analysis").click(function() {
$.ajax({
....
success: function(response) {
$("#exportExcelSales").submit(); //SUbmit the exportExcelSales form.
}
});
});
无需更改您的layouts/exportExcelSales
代码。