1

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?

2
  • Editing our url's to non wwww which is how their set in zend started to work, I see in chrome console "XHR finished loading:...." but no select list Commented Oct 12, 2011 at 17:38
  • chrome console shows XHR finished loading: "domain.com/account/ajaxautocomplete?format=json" it should have ?term=asdf being the letters I started to type but it's not picking that up, any ideas? Commented Oct 13, 2011 at 0:40

2 Answers 2

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
    });
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, are you able to get the row ID from your database and have the input set with that name/ID? And are you validating it with any code so users dont enter some value that's not coming from your db table?
In that case, I'd recommend using a standard Zend Form with select element and enhance it with the [Chosen plugin]( harvesthq.github.com/chosen)
-1

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

Try chrome.exe --disable-web-security

2 Comments

I don't think this is the answer there's no way all our website visitors are going to be doing this in chrome. And I don't see what file:// has to do with this? We're getting a list of categories from our mysql table, trying to turn it into an autocomplete through a zend action.
please forget about 'file://', I edited answer earlier :) Of course you visitors will not setup chrome. It is your web server that does not allow this type of requests. Try adding in response header something like header("Access-Control-Allow-Origin: http://domain.com")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.