【问题标题】:Loading data through Jquery ajax for datagrid?通过 Jquery ajax 为数据网格加载数据?
【发布时间】:2012-03-30 22:05:08
【问题描述】:

我有这段代码 sn-p(虽然不完整,忽略结束脚本标签)::

<script type="text/javascript">
function gotoa(){
    var h = $.get("http://localhost:8080/2_8_2012/jsp/GetJson.jsp", function(result) {

    });
    alert(result);


var myVar= h;
var storedata={
            identifier:"ID",
            label:"name",
            items: myVar
    };

var store = new dojo.data.ItemFileWriteStore({data: storedata}); 

GetJson.jsp 的代码是::

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="MyPackage.PopulateTextbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<%

String temp1;
PopulateTextbox obj = new PopulateTextbox();
temp1 = obj.method();
%>

<%=temp1 %>

</head>
<body>


</body>
</html>

我有一个 j 查询获取方法。我传入的 URL 返回一个 Json 数组字符串。 URL 的输出 ::

[{"ID":1,"Names":"Shantanu","Email":"shantanu.tomar@gmail.com"},{"ID":2,"Names":"Mayur","Email":"mayur.sharma@gmail.com"},{"ID":3,"Names":"Rohit"},{"ID":4,"Names":"Jasdeep"},{"ID":5,"Names":"Rakesh","Email":"rakesh.shukla@gmail.com"},{"ID":6,"Names":"Divyanshu"},{"ID":8,"Names":"hello"},{"ID":9,"Names":"fine"},{"ID":10,"Names":"shivani"}] 

现在我想要我的数据网格的这个输出,即我想要 var myVar 应该得到这个值,然后它将被传递给 dojo.data.ItemFileWriteStore。我无法这样做。请帮忙 ?谢谢。

【问题讨论】:

  • 我喜欢“请帮忙?”中的问号

标签: javascript ajax jquery datagrid


【解决方案1】:

$.get() 函数是使用 $.ajax() 函数的 AJAX 调用的简写 - 众所周知,AJAX 中的第一个 A 代表异步。如果要使用 AJAX 调用的响应执行代码,则需要将该代码放入传递给 $.get() 的回调函数中。

它可能看起来像这样(虽然没有完整的代码,我不能肯定地说:

function gotoa() {
    var store;

    var h = $.get("http://localhost:8080/2_8_2012/jsp/GetJson.jsp", function(result) {
        var myVar = result;
        var storedata={
            identifier:"ID",
            label:"name",
            items: myVar
        };

        store = new dojo.data.ItemFileWriteStore({data: storedata}); 
            // do whatever with store here, if necessary
    });
}

【讨论】:

  • 已经尝试过了 Anthony... 但不起作用.. 见上面的帖子... :(
【解决方案2】:

像这样修改你的脚本标签:

<script type="text/javascript">
function gotoa(){
    $.get("http://localhost:8080/2_8_2012/jsp/GetJson.jsp", function(result) {
        var storedata={
            identifier:"ID",
            label:"name",
            items: result
             };

            var store = new dojo.data.ItemFileWriteStore({data: storedata});
    });

}
</script>

GetJson.jsp 的代码是:

<%@page contentType="application/json" %>
<%@page import="MyPackage.PopulateTextbox" %>
<%
String temp1;
PopulateTextbox obj = new PopulateTextbox();
temp1 = obj.method();
%>

<%=temp1 %>

现在拨打gotoa() 它将初始化ajax调用并在回调函数中获取响应

【讨论】:

  • 我没有得到它 ryt Nemoy.. :( 它也没有显示任何错误。但在数据网格中也没有显示任何结果。我认为 items: 没有得到适当的值。但是当我打开 URL 我确实得到了我在我的问题中作为输出给出的 Json 值数组。可能是什么原因?如果我们将内容类型更改为 text/HTML 而不是 application/json 会有什么不同吗?谢谢。
  • 提醒回复并在此处发布,或者您可以公开链接吗?
  • 这是我打开链接 localhost:8080/2_8_2012/jsp/GetJson.jsp 和内容类型 text/html 时的输出:[{"ID":1,"Names":"Shantanu","Email":"shantanu.tomar @gmail.com"},{"ID":2,"Names":"Mayur","Email":"mayur.sharma@gmail.com"},{"ID":3,"Names":"Rohit "},{"ID":4,"Names":"Jasdeep"},{"ID":5,"Names":"Rakesh","Email":"rakesh.shukla@gmail.com"},{ "ID":6,"Names":"Divyanshu"},{"ID":8,"Names":"hello"}] 现在,当内容类型为 application/json 时,所有的 html 标签也一样body n all,输出放置在 body 标记之外,即放置 scriptlet 代码的位置。
  • 修改为$.get("http://localhost:8080/2_8_2012/jsp/GetJson.jsp", function(result) { alert(result); });,查看是否有告警
  • 可能是缓存,尝试缓存破坏$.get("http://localhost:8080/2_8_2012/jsp/GetJson.jsp?random=" + new Date().getTime(), function(result) { alert(result); });
猜你喜欢
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 2013-07-07
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
相关资源
最近更新 更多