2015-05-17 124 views
0

我有奇怪的問題:jQuery的自動完成與JSON數據

首先,我有這樣的代碼:

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'); 
     } 
    }); 
}; 

我的JSON是 - 功能DAJJSON()的返回:

[{"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"}] 

,並在那裏我測試我剛剛得到的代碼:

Uncaught SyntaxError: Unexpected token <

+0

'POST'是不提交數據的錯誤http操作...您可能想使用'GET'。這只是遵循HTTP規範。 – abc123

+0

@ abc123我必須在以前的工作中與Oracle Application Express進行交互,並且它允許的唯一請求是「POST」。我同意你 - 按照規範隨時隨地......當它不可能時,它可能是一個絕對的噩夢.. –

回答

2

在您使用的任何瀏覽器(通常是F12或Ctrl-Shift-I或在某處的菜單中)中打開開發人員工具。點擊「網絡」標籤並運行你的代碼。

查看對您的AJAX請求的響應。它不會是你所期待的:)

你傳遞給AJAX的dataType屬性告訴服務器你期待什麼樣的數據。您已將它設置爲html,因此您的回覆將以HTML格式顯示。從外觀上看,你真的想要json

+0

也許JSON是問題因爲我需要這種格式:var countries = [ {value:'Andorra',數據:'AD'}, // ... {value:'津巴布韋',數據:'ZZ'} ]; –