2013-04-23 71 views
6

我試圖發動一個XMLHttpRequest mongoDB通過AJAX檢索文檔。REST AJAX請求mongoDB

這是我的代碼:

function getJsonDocumentByModeldid(model_id) { 
    var valoreInput = document.getElementById('inputModelId').value; 
    alert(valoreInput); 

    $.ajax({ 
     url: "http://localhost:28017/test/", 
     type: "get", 
     //data: "filter_a=" + valoreInput, 
     dataType: 'jsonp', 
     crossDomain: true, 

     success: function (data) { 
     alert("success"); 
     //var json2javascript = $.parseJSON(data); 
     manageLayout(); 
     }, 

     error: function (XMLHttpRequest, textStatus, errorThrown) { 
     alert("Status: " + textStatus + " Error:" + errorThrown); 
     } 
    }); 
} 

我的函數總是返回一個錯誤。那麼問題是什麼?

+1

什麼錯誤? – andyb 2013-04-23 08:35:58

+0

ajax請求與腳本失敗,而不是如果我在瀏覽器中複製url,服務器響應成功。 我不知道是什麼問題... – ilamaiolo 2013-04-23 08:52:48

+0

警報錯誤是Jqueryxxxxxx沒有被調用! – ilamaiolo 2013-04-23 08:55:43

回答

6

,支持該功能爲Simple (read-only) REST Interface但部分進行跨域請求--jsonp否則你會受到Same origin policy問題,因爲你正在從請求不匹配的IP地址的IP地址和端口和mongoDB正在運行的端口。

開始mongoDBmongod.exe --rest --jsonp(加上您可能有的任何其他選項)。

以下示例頁面可通過網絡服務器(例如Apache HTTP Server)提供,或者簡單地保存在本地並作爲文件加載到瀏覽器中。該請求是關於dbCollection信息稱爲andyb,我在MongoDB中與創建第一:

db.createCollection('andyb'); 

HTML

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
    <title>mongoDB AJAX demo</title> 
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script> 
    <script type='text/javascript'>//<![CDATA[ 
    $(function(){ 
    $.ajax({ 
     url: 'http://localhost:28017/local/andyb', 
     type: 'get', 
     dataType: 'jsonp', 
     jsonp: 'jsonp', // mongod is expecting the parameter name to be called "jsonp" 
     success: function (data) { 
     console.log('success', data); 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
     console.log('error', errorThrown); 
     } 
    }); 
    });//]]> 
    </script> 
</head> 
<body> 
</body> 
</html> 

許多瀏覽器都支持CORS現在這是一種替代(更現代)方式來促進跨域資源。

+0

非常感謝您的回覆! – ilamaiolo 2013-04-26 16:36:48

0

以前的答案可以用延遲對象被修改(見本善導: 「How do I return the response from an asynchronous call?):

<!doctype html> 
<meta charset="utf-8"> 
<title>mongoDB AJAX demo</title> 
<script src="http://code.jquery.com/jquery-latest.min.js" ></script> 
<script>  
    $(function(){ 
    // add rest = true and jsonp = true to /etc/mongodb.conf, 
    // then restart mongodb 
     $.ajax({ 
       url: "http://localhost:28017/local/startup_log/", 
       type: 'get', 
       dataType: 'jsonp', 
       jsonp: 'jsonp', // mongodb is expecting that                      
      }) 
      .done(function(data) { 
       d=JSON.stringify(data,undefined,1); 
       $("code").text(d).css("color","green"); 
      }) 
      .fail(function(request,status,error) { 
       $("code").text("get failed: "+error).css("color","red"); 
      }) 
      .always(function() { 
       console.log("finished") 
      }) 

    }); 
</script> 
<body> 
    <pre> 
    <code> 
    </code> 
    </pre> 

總之,在這兩種情況下,只有當沒有提供端口error:fail()處理工程的,例如:

url:"localhost/asdasdasd" 

導致一個錯誤處理;而

url:"localhost:28017/asdasdasd" 

導致404登錄控制檯,像這樣:

GET http://localhost:28017/?jsonp=jQuery110207253803350031376_1383522587177&_=1383522587178 404 (OK)