简体   繁体   English

无法使用Ajax解析jquarymobile的html页面中的JSON输出

[英]Unable to parse JSON output in jquarymobile's html page using Ajax

I have developed screens for my android application using PhoneGap and jQuery Mobile. 我已经使用PhoneGap和jQuery Mobile为Android应用程序开发了屏幕。 Using below js and css files in my html page 在我的html页面中使用下面的js和css文件

 <link rel="stylesheet" href="css/jquery.mobile-1.3.1.css" />
 <script src="js/jquery-1.9.1.js" type="text/javascript"></script>
 <script src="js/jquery.mobile-1.3.1.js" type="text/javascript"></script>

Now, Screens are working fine and I need to bind data from db to my screen for that I have created web services that are giving me JSON outputs. 现在,屏幕运行良好,并且我需要将db中的数据绑定到屏幕,因为我已经创建了向我提供JSON输出的Web服务。 One of the JSON outputs that I'm trying to parse in html is as below. 我尝试在html中解析的JSON输出之一如下。

[
 {"rest_id": 35, "rest_name": "Just 10"},
 {"rest_id": 36, "rest_name": "Egg Zone"},
 {"rest_id": 37, "rest_name": "Tandoor & Curries"},
 {"rest_id": 38, "rest_name": "China Bite"}
]

Now, to my HTML Page. 现在,到我的HTML页面。 What I'm trying here is this 我在这里尝试的是

<script type="text/javascript">
        $.getJSON("MyURL", function( data ) {
            alert('success');
            /* My Logic for getting value from JSON output */
        });
     </script>

Issue here is I'm not even getting alert msg that I'm trying to print on the page. 这里的问题是我什至没有收到我要在页面上打印的警告消息。

I'm new to all these .. So if any1 can tell me where and what I'm doing wrong would be great help. 我对所有这些都是陌生的..因此,如果有任何人可以告诉我我做错了什么地方和做错了,那将是很大的帮助。

Thanks all 谢谢大家

Mayur 马约尔

Well, I am not familiar with PhoneGap but it seems in your script you have written code to get data. 好吧,我对PhoneGap并不熟悉,但是似乎在脚本中您已经编写了获取数据的代码。 There should be something about request too. 也应该有一些关于请求的信息。 As Ajax is about request and response. 由于Ajax是关于请求和响应的。

  $.ajax({
  url : '${resourceURL}',
  data : data,//send Request 
  type: 'POST',
  dataType : "json",
  success : function(data) {
    // something my be alert(success)
  }
});

From the code you provided it seems you are not calling that $.getJSON() function. 从您提供的代码来看,您似乎没有在调用$.getJSON()函数。 You need to call it somehow (from some event listener for example) or you can execute it when DOM is ready by 您需要以某种方式调用它(例如,从某个事件侦听器),或者可以在DOM准备就绪时执行它,方法是:

$(function(){
  $.getJSON("MyURL",
    function(data){
      alert('success');
      /* My Logic for getting value from JSON output */
    }
  );
});

just to see if it is working. 只是看看它是否在工作。

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

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