首页 > 解决方案 > 当另一个成功执行时进行一个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 传递商店名称。

任何帮助,将不胜感激

谢谢!

标签: phpjquery

解决方案


推荐阅读