當我在ajax url中傳遞json請求時發生錯誤。我已經在ajax中傳遞了200以上的數據。我的所有數據的來自 「/搜索/ searchendpoint」 URL當我在AJAX url中傳遞JSON值時發生錯誤
控制器:
$searchitem = $this->MbPriceList->find('all' , [
'fields' => [
'id',
'name' => 't1.item_name'
],
'join' => [
'table' => 'mb_item_list',
'alias' => 't1',
'type' => 'INNER',
'conditions' => [
't1.item_code = MbPriceList.item_code'
]
]
]) ->toArray();
$this->set([
'response' => $searchitem,
'_serialize' => ['response']
]);
JSON請求:
<script>
var myUrl = "/search/searchendpoint";
$.mockjax({
url: myUrl,
dataType: "json",
type: "get",
data: JSON.stringify(myUrl),
contentType: 'application/json; charset=utf-8',
response: function(data){
alert(data)
}
});
</script>
<script>
$('#search').typeahead({
ajax: '/search/searchendpoint'
});
</script>
錯誤:
jquery-2.2.4.min.js:2 Uncaught TypeError: Cannot use 'in' operator to
search for 'length' in "/search/searchendpoint" at s
(jquery-2.2.4.min.js:2) at Function.each (jquery-2.2.4.min.js:2) at
isMockDataEqual (jquery.mockjax.js:67) at getMockForRequest
(jquery.mockjax.js:119) at Function.handleAjax [as ajax]
(jquery.mockjax.js:444) at Typeahead.execute
(bootstrap-typeahead.js:170) at f (jquery-2.2.4.min.js:2)
我想這可能是您的回覆數據不是預先輸入的格式。查看控制器生成的實際JSON會很有幫助。還有mockjax進入這個地方嗎?你在測試真正的控制器,還是不是? – ADyson
是的,我已經測試了我的控制器.. json值顯示正確 – ganesh
所以這個結果發生在使用mockjax而不是真正的代碼?在這種情況下,你爲什麼向我們展示控制器代碼?如果你嘲笑ajax調用,它不會被使用。你的mockjax定義似乎缺乏任何一種模擬結果對象,也許就是這個問題。 – ADyson