jQuery 自动完成 mysql zend 操作/控制器没有响应?

发布于 2024-12-09 12:16:31 字数 1208 浏览 0 评论 0原文

我们在 zend 中使用 jquery 自动完成并设置我们的操作,如下所示

public function ajaxautocompleteAction()
{
    $postData = $this->_request->getParams();
    $term = $postData['term'];
    $categoryObj = new Categories();
    $result = $categoryObj->searchCategory($term);
    $this->view->result = $result;
}

视图文件中的 javascript 是这样的

    $(function() {
        var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
        $( "#autotest" ).autocomplete({
            minLength: 2,
            source: function(request, response){
                var iterm = request.term;
                var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
                $.post( url, {term: iterm},
                function( data ) {
            response(data); });
            }
    });
});

在 chrome 控制台中我收到此错误

XMLHttpRequest 无法加载 http://www.domain.com/account/ajaxautocomplete?format=json。 Access-Control-Allow-Origin 不允许来源 http://domain.com

有什么想法为什么没有从 ajax 请求中得到结果吗?

Were using jquery autocomplete in zend and setup our action like this

public function ajaxautocompleteAction()
{
    $postData = $this->_request->getParams();
    $term = $postData['term'];
    $categoryObj = new Categories();
    $result = $categoryObj->searchCategory($term);
    $this->view->result = $result;
}

The javascript in the view file is this

    $(function() {
        var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
        $( "#autotest" ).autocomplete({
            minLength: 2,
            source: function(request, response){
                var iterm = request.term;
                var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
                $.post( url, {term: iterm},
                function( data ) {
            response(data); });
            }
    });
});

In chrome console i get this error

XMLHttpRequest cannot load http://www.domain.com/account/ajaxautocomplete?format=json. Origin http://domain.com is not allowed by Access-Control-Allow-Origin.

Any ideas why were not getting results from the ajax request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俏︾媚 2024-12-16 12:16:31

这就是我之前使用 jQueryUI 的自动完成和 ZF 的方式...

  1. 创建您的操作

    公共函数ajaxautocompleteAction()
    {
        $term = $this->getRequest()->getParam('term');
        $categoryObj = 新类别();
        $结果 = $categoryObj->searchCategory($term);
        $this->视图->结果 = $结果;
    }
    
  2. 将 AjaxContext 添加到您的操作,禁用自动 JSON 序列化。我跳过自动序列化,因为您的模型并不常见来表示 jQueryUI 的自动完成功能查找的常见“标签”/“值”对

    公共函数init()
    {
        $this->_helper->ajaxContext->addActionContext('ajaxautocomplete', 'json')
                                   ->setAutoJsonSerialization(false)
                                   ->initContext('json');
    }
    
  3. 创建 JSON 视图 (views/scripts/account/ajaxautocomplete.json .phtml)

    <前><代码>结果为$category) {
    // 格式化每个结果以实现 jQueryUI 自动完成
    $数据[] = 数组(
    '标签' => $category->getName(),
    '值' => $类别->getName()
    );
    }
    回声 Zend_Json::encode($data);

  4. 将自动完成操作的 URL 作为 JavaScript 变量添加到视图中需要使用它(假设您在布局中使用 HeadScript 助手)

    $this->headScript()->prependScript(sprintf('var searchUrl = "%s";',
        $this->url(数组(
            '行动' => 'ajax自动完成',
            '控制器' => '帐户'
        ), null, true)));
    
  5. 像这样设置 JavaScript

    $("#autotest").autocomplete({
        来源:搜索网址,
        最小长度:2
    });
    

This is how I've used jQueryUI's autocomplete and ZF before...

  1. Create your action

    public function ajaxautocompleteAction()
    {
        $term = $this->getRequest()->getParam('term');
        $categoryObj = new Categories();
        $result = $categoryObj->searchCategory($term);
        $this->view->result = $result;
    }
    
  2. Add an AjaxContext to your action, disabling automatic JSON serialisation. I'm skipping the auto serialisation as it's not common for your models to represent the usual "label" / "value" pairs jQueryUI's automcomplete looks for

    public function init()
    {
        $this->_helper->ajaxContext->addActionContext('ajaxautocomplete', 'json')
                                   ->setAutoJsonSerialization(false)
                                   ->initContext('json');
    }
    
  3. Create your JSON view (views/scripts/account/ajaxautocomplete.json.phtml)

    <?php
    $data = array();
    foreach ($this->results as $category) {
        // format each result for jQueryUI autocomplete
        $data[] = array(
            'label' => $category->getName(),
            'value' => $category->getName()
        );            
    }
    echo Zend_Json::encode($data);
    
  4. Add the URL for your autocomplete action as a JavaScript variable to the view that needs to use it (assuming you use the HeadScript helper in your layout)

    $this->headScript()->prependScript(sprintf('var searchUrl = "%s";',
        $this->url(array(
            'action'     => 'ajaxautocomplete',
            'controller' => 'account'
        ), null, true)));
    
  5. Setup your JavaScript like this

    $("#autotest").autocomplete({
        source: searchUrl,
        minLength: 2
    });
    
花伊自在美 2024-12-16 12:16:31

貌似domain.com不允许跨域调用。

尝试 chrome.exe --disable-web-security

looks like domain.com does not allow cross-domain calls.

Try chrome.exe --disable-web-security

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文