2014-09-21 63 views
0

我知道有關於「parsererror」的一百萬個問題,但似乎無法找到對我的情況有意義的答案。這裏是我的病懨懨的代碼:jQuery/AJAX/parsererror/XAMPP/Win8.1/IE11/Chrome

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<title>Test</title> 

<script type="text/javascript" src="jquery-2.1.1.min.js"></script> 

<script type="text/javascript"> 
    $.ajax({ 
     url: 'http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0', 
     dataType: 'jsonp', 
     contentType: 'application/jsonp; charset=utf-8', 
     jsonp: 'onscriptload', 
     success: function(data, textStatus) 
      { 
       console.log("Success."); 
       var MM = Microsoft.Maps; 
       var map = new MM.Map($('#mapDiv')[0], 
        { 
         credentials: 'a valid bing maps key' 
        }); 
      }, 
     error: function(xOptions, textStatus) 
      { 
       console.log(JSON.stringify(xOptions) + ' ' + textStatus); 
      } 
     }); 
</script> 
</head> 
<body> 
    <div id="mapDiv" class="map"></div> 
</body> 
</html> 

的「錯誤」部分被觸發,並返回{"readyState":4,"status":200,"statusText":"load"} parsererror,和我難倒試圖得到這個工作。在IE11和Chrome中完全相同的錯誤/行爲。這是最有用的文章(Callback function for JSONP with JQuery ajax),但「onscriptload」應該已經是一個內置函數,我不應該(重新)定義/重載,不是嗎?任何幫助讚賞...

+0

那個url是一個腳本。你爲什麼試圖把它當作jsonp? – charlietfl 2014-09-21 23:29:52

+0

我試圖按照代碼示例[這裏](http://build-failed.blogspot.com/2012/02/bing-maps-nodejs-websockets-with.html) – 2014-09-22 10:23:45

回答

0

該ajax請求是一個「黑客」,以便加載冰點地圖庫按需而不是頁面加載。我會推薦一個典型的方法,如:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script> 

     <script type="text/javascript"> 

      function loadMap() { 
       // Initialize the map 
       var map = new Microsoft.Maps.Map(document.getElementById("map"), 
       { 
        credentials: "BING MAPS KEY HERE" 
       }); 

       //load the pushpins here 
      } 

     </script> 
    </head> 
    <body onload="loadMap();"> 
     <div id="map"></div> 
    </body> 
</html>