0

I need to fill the field .txtProvince with the province name, and the field .txtProvinceHidden with the province ID (or .txtProvince can have a 'province_id' attr, maybe).

I found this solution (here):

$(function() {                  
    var provinces = [{id: 1,label: "Alagoas"},{id: 2,label: "Bahia"}];
    $(".txtProvince").autocomplete({
        source: provinces,
        select: function(event,ui) {
              $(this).val(ui.label);
              $(".txtProvinceHidden").val(ui.id);
        }                   
    });             
});

So far so good, now comes the problem: when the jquery ui performs the search, i get "forbidden" error.

Here is the url where it seeks to:

http://dicasdedesconto.localhost/[["1","Alagoas"],["2","Bahia"]?term=Ba

And here the error that I see on console:

Forbidden

You don't have permission to access /[["1","Alagoas"],["2","Bahia"] on this server.

Apache/2.2.20 (Ubuntu) Server at dicasdedesconto.localhost Port 80

I'm sure that is a route problem, but i don't know how to solve this. Has anyone had this problem?

Community
  • 1
  • 1
cbacelar
  • 545
  • 7
  • 20

1 Answers1

0

Here is a working example of your code: jsfiddle

Did you edit your example at all? If the object you're passing to the source is malformed, it may be interpreting it as a string, instead of an array of objects. Looking at the example of your console, it appears its trying to use your source as a path. Made me wonder if you might have fixed your own problem when posting this.

From the Autocomplete docs

When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data.

kmfk
  • 3,821
  • 2
  • 22
  • 32
  • ok... very good.. i edited the example.. and, really my json is `[[ id: 1, label: "Alagoas" ], [ id: 2, label: "Bahia"]]` instead of `[{ id: 1, label: "Alagoas" }, { id: 2, label: "Bahia"}];` ... is there a way to change this easily with js or php? – cbacelar Apr 19 '12 at 17:28
  • how is the json being provided? If you are creating it from PHP, its easiest to just build either a stdClass object or an associative array and `json_encode` it – kmfk Apr 19 '12 at 17:34
  • ok .. i was using `json_encode` .. so php hands me: `[{"id":"1","label":"Alagoas"},{"id":"2","label":"Bahia"}` ... and i decode with js `provinces = JSON.parse(result)` .. i'm alerting this and show `[object Object],[object Object]` .. when i alert the "province" var of the example, i see the same thing. what can be wrong?!?! – cbacelar Apr 19 '12 at 18:57
  • Dont decode it in your JS. leave it as a JSON string. `provinces = result;` – kmfk Apr 19 '12 at 19:28
  • OK.. It was my mistake.. i was creating the array with the keys "id" and "name", instead of "label" (unknowingly). but the main problem was, in fact, malformed source, like you said, and changing the array structure work.. thank you very very much * the source must be decoded on js – cbacelar Apr 19 '12 at 20:16