php - 当另一个成功执行时进行一个ajax调用
问题描述
当一个成功执行以将 post 变量传递给另一个控制器操作时,我正在尝试进行另一个 ajax 调用。但是,当我检查控制台日志消息时,它返回 null。我不确定为什么。
这是我的代码:
jQuery:
$('#modify-store-name').on('change', function() {
$.ajax({
type: "POST",
url: "/user/get-one-store",
dataType: "json",
data: {
store_name: $(this).val()
}
}).done(function (msg) {
$.each(msg, function (i) {
$('#modify-store-label').attr('style', '');
$('#modify-store-desc').attr('style', '');
$('#modify-store-category-label').attr('style', '');
$('#modify-store-category').attr('style', '');
$('.upload-btn-wrapper').attr('style', '');
$('#modify-store-desc').val(msg[i].store_description);
$('#modify-store-category').html($("<option />").val(msg[i].store_category).text(msg[i].store_category));
$('#msubmit').attr('disabled', false);
});
$.ajax({
type: "POST",
url: "/user/modify-store",
dataType: "json",
data: {
store_name2: $('#modify-store-name').val() // why is this sending a null value
}
}).done(function(msg) {
console.log(msg);
}).fail(function(msg) {
console.log(msg);
});
}).fail(function (msg) {
$("#msg").html(msg.failure);
});
});
和我的php代码:
public function getonestoreAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
try {
$store_name = $this->params()->fromPost('store_name');
echo json_encode($this->getUserService()->getAStore($store_name));
} catch (\Exception $e) {
echo json_encode(array('failure' => $e->getMessage()));
}
return $view_model;
}
public function modifystoreAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
if ($this->getRequest()->isPost()) {
try {
$store_name = $this->params()->fromPost('store-name2');
echo json_encode($store_name); // returning null
$mstore_name = $this->params()->fromPost('modify-store-name');
$mstore_description = $this->params()->fromPost('modify-store-description');
$mstore_category = $this->params()->fromPost('modify-store-category');
$mstore_image = $this->params()->fromFiles('modify-store-image');
if (count($mstore_image) > 0) {
if ($this->getUserService()->modifyStore($store_name, array('store_name' => $mstore_name, 'store_description' => $mstore_description, 'store_category' => $mstore_category, 'store_image' => $mstore_image, 'tmp_name' => $mstore_image['tmp_name']))) {
echo json_encode(array('success' => 'Store was modified successfully.'));
}
}
} catch (\Exception $e) {
echo json_encode(array('failure' => $e->getMessage()));
}
}
return $view_model;
}
我读到您可以像这样进行两个 ajax 调用,但我不确定为什么一个不通过 post 传递商店名称。
任何帮助,将不胜感激
谢谢!
解决方案
推荐阅读
- python - AZURE ML,python 代码获取:'tls_process_server_certificate','证书验证失败'
- excel - VBA 运行时错误。'1004':复制区域和粘贴区域的大小不同
- python - 从睡眠模式通过python代码打开监视器
- python - 以一个模式开始但不以另一个模式结束的正则表达式
- spring - Spring Boot Starter:重试事务问题
- c# - C#将字符串[]中的值插入模型列表
- html - 为什么我的反应应用程序中的按钮溢出了 div 的边缘?
- asp.net-web-api - 具有 OData $count 的 .Net Core 3 Web API 未返回值
- excel - 你如何粘贴唯一值而不显示被跳过的行?
- excel - 遍历文件夹中的文件,将内容发布到主文件中的空列,对于主文件新行中的每个源文件