简体   繁体   English

jQuery ajax load()函数

[英]jquery ajax load() function

I don't know why the jquery ajax load function is not responding. 我不知道为什么jquery ajax加载功能没有响应。 When I click on the link that should force the div to load contents of .txt file nothing happens except I get the alert window that indicates the load function ran successfully. 当我单击应强制div加载.txt文件内容的链接时,除了显示提示成功运行加载窗口的警报窗口外,什么都没有发生。 Here's the code: 这是代码:

mainTemplate.html mainTemplate.html

<div class="contentWrapper">
        <table>
            <thead>
                <tr>
                    <th>
                        Location
                    </th>
                    <th>
                        Account
                    </th>
                    <th>
                        Industry
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr id="Row1">
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                </tr>
                <tr id="Row2">
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                </tr>
                <tr id="Row3">
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                </tr>
                <tr id="Row4">
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                    <td>
                        foo
                    </td>
                </tr>
            </tbody>
        </table>
        <!-- <button>Click Me</button> -->
</div>

The jQuery file: jQuery文件:

$(document).ready(function () {

$("[id^=Row]").click(
    function () {
    $(".contentWrapper").load('foo.txt', function () { alert("Load was performed successfully")});
    }
);
});

foo.txt: foo.txt:

<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>

I'm frustrated here. 我在这里很沮丧。 I'm using Google Chrome as my browser. 我使用Google Chrome浏览器。

If you look closely at .load method documentation, you notice that its second parameter is a function of three parameters, which is called always when the request was "complete" which doesn't yet mean that it was successful. 如果仔细查看.load方法文档,则会注意到它的第二个参数是三个参数的函数,当请求“完成”时始终调用该参数,但这并不意味着请求成功。 See examples at the bottom of this page to see how to get error information from the request object 请参阅此页面底部的示例,以了解如何从请求对象获取错误信息。

It turns out I was using jQuery AJAX in the wrong way. 原来我以错误的方式使用了jQuery AJAX。 I wasn't running the code on a server and so when I clicked on a link to run the jQuery load() function the xml http request didn't go through. 我没有在服务器上运行代码,因此当我单击链接来运行jQuery load()函数时,xml http请求没有通过。 Rookie mistake. 新秀错误。

I ran the code on the server and fixed the file address issue. 我在服务器上运行了代码,并修复了文件地址问题。 It works now! 现在可以使用!

Thanks for all the help everyone! 谢谢各位的帮助!

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

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