如何使用AJAX来加载外部网页 [英] How to load external page using ajax

查看:171
本文介绍了如何使用AJAX来加载外部网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该链接到另一个页面的链接按钮。该网页的内容应当显示的当前页。我使用的引导框架,但是我创建的函数似乎并没有工作。

这是我的code;

 < A HREF =ID =瑞思灵>链接1< / A>

< D​​IV CLASS =span8ID =maincont>< / DIV>
 

JavaScript的;

  $('#瑞思灵)。点击(函数(){
    $阿贾克斯({
        键入:GET,
        网址:sample.html,
        数据: { },
        成功:功能(数据){
            $('#maincont')的HTML(数据)。
        }
    });

});
 

解决方案

您以下几个方面。

第一

如下

使用HTML,。

 < A HREF =JavaScript的:无效(0);的id =瑞思灵> LINK1和所述; / a取代;
 

的Javascript:无效(0)工作,将返回false从剧本

不要更改HTML code什么,但做出如下改变的jQuery code。

  $('#瑞思灵)。点击(函数(五){
    即preventDefault();
    $阿贾克斯({
        键入:GET,
        网址:sample.html,
        数据: { },
        成功:功能(数据){
            $('#maincont')的HTML(数据)。
        }
    });
});
 

电子preventDefault(); prevent的默认行为 HTML 这样你就可以执行你的code。

i have a link button that supposed to link to another page. The content of that page should be displayed to the current page. I'm using the bootstrap framework, however the function I created doesn't seem to work.

here's my code;

<a href="" id="reslink">link1</a>

<div class="span8" id="maincont"></div>

javascript;

$('#reslink').click(function(){
    $.ajax({
        type: "GET",
        url: "sample.html",
        data: { },
        success: function(data){
            $('#maincont').html(data);
        }
    });

});

解决方案

You have following ways.

First

use html as below,.

<a href="Javascript:void(0);" id="reslink">link1</a>

Javascript:void(0); work as return false from script.

Second

Do not change anything in html code but make below change in jQuery code.

$('#reslink').click(function(e){
    e.preventDefault();
    $.ajax({
        type: "GET",
        url: "sample.html",
        data: { },
        success: function(data){
            $('#maincont').html(data);
        }
    });
});

e.preventDefault(); prevent the default behavior of the HTML so you can execute your code.

这篇关于如何使用AJAX来加载外部网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆