jQuery / AJAX / parsererror / XAMPP / Win8.1 / IE11 / Chrome

时间:2014-09-21 23:18:40

标签: javascript jquery ajax parse-error

我知道有一百万个关于“parsererror”的问题,但似乎找不到对我的情况有意义的答案。这是我生病的代码:

<!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”应该已经是一个内置函数,我不应该(重新)定义/重载,不是吗?任何帮助表示赞赏...

1 个答案:

答案 0 :(得分:0)

ajax请求是&#34; hack&#34;为了按需加载bing maps lib而不加载页面加载。我建议采用一种典型的方法:

<!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>