简体   繁体   English

无法在ajax调用中加载资源

[英]Failed to load resource in ajax call

Consider this code: 考虑以下代码:

  function drawCharts2() {
  var jsonData2 = $.ajax({
      url: "http://**.**.**.**:9001/jolokia/read/*",
      dataType:"json",
      async: false
      }).responseText;

     var parsed2 = JSON.parse(jsonData2);

The above URL contains no value at the startup of the web app. 上述网址在网络应用启动时不包含任何值。 The data in that url will come after 2-3 minutes. 该网址中的数据将在2-3分钟后显示。

When I start my web app. 当我启动我的Web应用程序时。 It gives error, but as a workaround I have set my page refresh time to 30 seconds. 它给出了错误,但是作为一种解决方法,我将页面刷新时间设置为30秒。 So after 30 seconds the table generates successfully. 因此,表在30秒后成功生成。

I need a way so that, even if the url will not contain any data, the code should not give any error. 我需要一种方法,即使该URL不包含任何数据,该代码也不应给出任何错误。 It should just skip the whole code. 它应该只跳过整个代码。 How can I achieve this? 我该如何实现?

Try this 尝试这个

    var parsed2;
        function drawCharts2() {
                $.ajax({
                    type: "GET",
                    url: "http://**.**.**.**:9001/jolokia/read/*",
                    contentType: "application/json; charset=utf-8",            
                    dataType: "json",
                    success: function (response) {
                     if(response) {
                       parsed2 = response;
                     } 
                    else
                         {   drawCharts2();  // retry if you want to..     }
                    },
                    error: function (event) {

                        alert("Transmission Failed. (An error has occurred)");
                    }
                });
      if( parsed2 != null)
      {
      var arr2 = []; for(var x in parsed2)
      { arr2.push(parsed2[x]);
        } 
        var array_keys = new Array(); 
        var array_values = new Array();
         }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM