2013-09-28 106 views
0

我的Jquery代碼如下,但它總是給'加載失敗的內容',我需要做一個Ajax調用來獲取數據使用rest api。我正在使用jQuery 1.3.2。AJAX調用:「無法加載內容」

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 


$(function() { 

var cat_id = ${coreNode.ancestors[0].id}; 
var restUrl = "http://xyz./restapi/vc/categories/id/"+cat_id+"/boards"; 
$.ajax({ 
    type: "POST", 
    url: restUrl, 
    contentType: "text/xml; charset=utf-8", 
    data: "DATA", 
    success: function (response) { 

    alert(restUrl); 



     }); 



    }, 
    error: function (response) { 

     $('#float-threads').html('Failed to load the content'); 

    } 
}); 
}); 

</script> 

<div id="float-threads"> 
</div> 
+0

什麼錯誤? url是否可以返回404? – Sumurai8

+0

錯誤出現在url:'restUrl'中,請刪除該報價。和數據:「數據」,你需要將數據作爲變量傳遞給它並賦值。 –

+0

另外,如果你的目的是簡單地從服務器獲取數據,你應該使用GET而不是POST;) – Terry

回答

4

您正在傳遞字符串'restUrl'而不是變量的值。

刪除報價url: 'restUrl',

使用url: restUrl,

與您的數據。它應該在之前定義並且不加引號。否則php將收到一個字符串(單詞),而不是該變量的內容/值。

在你的成功函數中,你有一個額外的});,不應該在那裏。 (也許你會有其他代碼的一部分)。因此,使用:

success: function (response) { 
    alert(restUrl); 
// }); - without this line 
}, 
+0

編輯它,但它現在給出錯誤}); alert(restUrl)後; –

+0

@VarunLuthra,你從哪裏得到'});'alert()from?之後?我認爲你應該刪除它。 – Sergio

+0

檢查你的代碼是否與[JSLint](http://www.jslint.com/)一致 - 有時它可能會拋出錯誤,因爲你的括號沒有正確關閉;) – Terry

0

嘗試這樣的事情

var cat_id = ${coreNode.ancestors[0].id}; 

,而不是嘗試一些硬編碼的值傳遞給它,然後檢查

var cat_id = 1;