【问题标题】:Cannot load external URL using a simple jQuery dataTables无法使用简单的 jQuery 数据表加载外部 URL
【发布时间】:2012-02-09 05:26:01
【问题描述】:

我在 Eclipse 中创建了一个简单的静态 Web 项目。我将 jquery.js 和 jquery.dataTables.js 下载到我的 WebContent 文件夹中。然后我创建了一个 index.html 页面,如下所示:

<!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=UTF-8">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.dataTables.js"></script>

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#example').dataTable({
            "sAjaxSource" : 'http://localhost:8080/myProj/listLogs?messageId=33333333333'
        });
    });
</script>


<title>Insert title here</title>
</head>
<body>

    <table cellpadding="0" cellspacing="0" border="0" class="display"
        id="example">
        <thead>
            <tr>
                <th>stuff</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
        <tfoot>
            <tr>
                <th>stuff</th>
            </tr>
        </tfoot>
    </table>

</body>
</html>

来自http://localhost:8080/myProj/listLogs?messageId=33333333333 的 JSON 响应应如下所示:

{

"aaData": [
    [
        "This is a NEW message"
    ],
    [
        "Parsing message with messageId = 33333333333 and eventType = CREATE"
    ],
    [
        "Start running workflow with 4 actions"
    ],
    [
        "Updating entitlement for event: [33333333333:CREATE]"
    ],

... }

但是当我在 Firefox 中打开 index.html 时,什么都没有加载。我确实在页面上看到了这一点:

stuff
stuff
Loading...
Showing 0 to 0 of 0 entries
PreviousNext

我的 JSON URL 甚至没有被调用。不知道为什么。

【问题讨论】:

  • 使用JSONLint.com验证你的json无效
  • 刚刚做了。这是有效的。正如我所说,甚至没有调用 URL。我可以通过服务器端的监控来判断。
  • 我建议您查看 firebug 控制台或来自 firefox 网络面板下 XHR 选项卡的 XHR 请求,以查看您的 URL 调用是否导致某些错误,例如 500 内部服务器错误或 404 错误将帮助您并检查拍拍是否正确
  • 附带说明,使用 jQuery,您不再需要 $(document).ready(function() {。只需简单地将 onload 代码包含在:$(function(){ //code goes here });
  • firebug 控制台说:GET localhost:8080/myProj/… 200 OK x 254ms。嗯...似乎该 URL 确实被正确调用了。页面上仍然没有显示任何内容。

标签: jquery html json datatables


【解决方案1】:

我想我找到了答案:

http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

我的 JSON 端点在不同的端口上 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2018-06-03
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多