【问题标题】:Ajax response is going wrong wayAjax 响应出错了
【发布时间】:2012-12-10 23:04:53
【问题描述】:

任务:当我从选择标签客户(我有客户ID)中选择时,它必须将请求输入数据库并返回所有客户字段。然后它必须自动填充一些输入。我尝试制作 ajax+jQuery。阿贾克斯不错。它现在可以工作了!

这里是 JS:

我想通了:

<script>
$(document).ready(function() {
    $('#customer_load').change(function() {
        $.ajax({
            url: '<?= $this->url(array('action' => 'ajax', 'controller' => 'baza')) ?>',
            type: 'POST',
            dataType: 'json',
            data: {
                    // list of request parameters 
                    'customer_id':  $(this).attr('value')
            },
            success: function(data) {
                    //alert(data.current_discount);
                    $('#extra_discount').val(data.extra_discount);
                    $('#current_discount').val(data.current_discount);
                    $('#customer_number').val(data.customer_id);
            }
        });
    });
});

PHP 初始化:

$this->_helper->AjaxContext()->addActionContext('add', 'json')->initContext('json');

Ajax 动作:

$id= $this->_getParam('customer_id');
$result = $this->_customers->fetchSelected($id);
$this->view->customers = $result;
$this->_helper->json($result);

html:

<select name="customer_id" id="customer_load" style="width:300px;">
   <option value="0">Выберите заказчика</option>
      ?php foreach ($this->customers as $cus): ?>
   <option value="<?= $cus['customer_id'] ?>"" <?php if ($cus['customer_id'] == $this->form_data['customer_id']) echo "selected"; ?> ><?= $cus['lastname'] . " " . $cus['name'] ?></option>
   <?php endforeach; ?>
    <option value="new" onclick="NewCustomer()">Новый заказчик</option>
    </select>

【问题讨论】:

  • 您是否使用某种 mod-rewrite 来解析 Ajax 调用“/add”中的给定 URL?
  • 暂时禁用isXmlHttpRequest 签入控制器并首先在浏览器中直接测试您的ajax 操作。一旦你让它工作,添加安全条件并实现你的 ajax 调用。
  • 感谢 Alex 的建议,我将 url 更改为 '/baza/add',但在我的萤火虫中它是 POST voskmodel.dev/baza/add 200 OK 65ms 我猜 customer_id 无处发送
  • 暂时将$this-&gt;getRequest()-&gt;getPost更改为$this-&gt;_getParam并在浏览器中这样www.mysite.com/controller/action/customer_id/123请求

标签: php jquery ajax zend-framework post


【解决方案1】:

从您的帖子中很难理解问题是在客户端还是服务器端...... 在您的第一个示例中,您没有在 ajax 请求中使用 customer_id,并且您不需要在 javascript 中将值转换为 Number

在下面使用 AJAX 请求:

$(document).ready(function(){
   $.ajax({
     url: <?= $this->url(array('action' => 'add', 'controller' => 'baza')) ?>,
     type: 'POST',
     dataType: 'json',
     data: {
        // list of request parameters 
        'customer_id': $('select[name=customer_id] option:selected').val(),
     },
     success: function(results){
         // analyze your response and add custom logic
         console.debug(result);
     }
   });
});

根据您的 PHP 代码,您将事情复杂化了。 在操作顶部添加您的检查并在您尝试使其工作时将其注释掉(这样您可以直接在浏览器中测试baza/add),一旦您得到它的工作取消注释和测试。使用JSON view helper输出json。

   public function addAction() 
   {
        // checks/validation/etc

        // do some processing...               
        $result = $this->_customers->fetchSelected($id);

        // Send the JSON response:
        $this->_helper->json($result);
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2017-05-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多