milemilic mile
milemilic mile

Reputation: 87

Jquery autocomplete with json data

I have strange problem:

First I have this code:

var countries = dajjson();

$('#usluga').autocomplete({
    lookup: countries,
    onSelect: function (suggestion) {
        alert('You selected: ' + suggestion.ID + ', ' + suggestion.naziv);
    }
});
});

function dajjson() {
    $.ajax({
        url: "autoUsluge.php",
        type: "POST",
        async: true, 
        dataType: "html",

        success: function(data) {
            console.log(data);   
        }, 
        error: function (data) {
            console.log(data);
            console.log('GRESKA NEKA');
        }
    });
};

My JSON is - function DAJJSON() return:

[{"ID":"4","naziv":"","opis":"Web dizajn","jmere":"komada","kol":"2","cena":"50","valuta":"Eur","popust":"5","porez":"20","user_id":"1"},{"ID":"5","naziv":"","opis":"Programiranje","jmere":"sati","kol":"5","cena":"10","valuta":"Eur","popust":"5","porez":"20","user_id":"1"},{"ID":"6","naziv":"","opis":"Popravka zadnjeg trapa na automobilu Audi","jmere":"komada","kol":"1","cena":"80","valuta":"Eur","popust":"5","porez":"20","user_id":"1"}]

and where I test the code I just get:

Uncaught SyntaxError: Unexpected token <

Upvotes: 0

Views: 46

Answers (1)

Alex McMillan
Alex McMillan

Reputation: 17962

Open up developer tools in whatever browser you're using (usually F12 or Ctrl-Shift-I or in the menus somewhere). Click the "Network" tab and run your code.

Look at the response to your AJAX request. It won't be what you're expecting :)

The dataType property you're passing to AJAX tells the server what kind of data you're expecting. You have it set as html, so your response will be in HTML. By the looks of it, you really want json.

Upvotes: 2

Related Questions