简体   繁体   English

jQuery ajax()函数-内容无法加载

[英]jQuery ajax() function - content doesn't load

I'm using jQuery and I've got problem with ajax() function. 我正在使用jQuery,而ajax()函数存在问题。 It doesn't seem to be a difficult problem but anyway I don't know what's going on. 这似乎不是一个难题,但无论如何我都不知道发生了什么。 I'd like to load content from another html file using ajax, here's my code: 我想使用ajax从另一个html文件加载内容,这是我的代码:

$(function(){
$('.submit-1').click(function(){
    $('.right').load('ajax.html .page-2');
});});

It works perfectly when I run it on NetBeans IDE - when I click the button, content loads. 当我在NetBeans IDE上运行它时,它可以完美工作-单击该按钮时,将加载内容。 The problem is that it doesn't work without Netbeans, for example - when I open index.html. 问题是,例如,当我打开index.html时,没有Netbeans就无法正常工作。

Run your code in a server(apache,nginx), ajax cannot run without a server 在服务器(apache,nginx)中运行您的代码,如果没有服务器,ajax将无法运行

use prevent default if your element is a link or a submit button 如果您的元素是链接或提交按钮,则使用prevent default

$(function(){
$('.submit-1').click(function(e){
    e.preventDefault();
    $('.right').load('ajax.html .page-2');
});});

Use preventDefault function. 使用preventDefault函数。 hope it will help 希望对你有帮助

$(function(){
 $('.submit-1').click(function(e){
 e.preventDefault();
 $('.right').load('ajax.html .page-2');
});});

Maybe it solves your problem when you installing a server. 也许可以解决安装服务器时的问题。 With Apache it's very easy to do this. 使用Apache可以很容易地做到这一点。 Just install it and configure/start Apache. 只需安装它并配置/启动Apache。 With XAMPP you can also install a Database and it supports PHP and PERL. 使用XAMPP,您还可以安装数据库,它支持PHP和PERL。

you can try this 你可以试试这个

$('.submit-1').click(function(){
    $.get( "ajax/test.html", function( data ) {
        $( ".result" ).html( data );
        alert( "Load was performed." );
    });
});

This is the solution 这是解决方案

$(function(){
$('.submit-1').click(function(){     
$.get('yourURL/home.html', function (data) {
    $('.right').append(data);
   });
});

you need to append the data retuned from server. 您需要附加从服务器重新调整的数据。 The 'data' variable in the call back function holds the complete html from home.html 回调函数中的“数据”变量包含home.html中的完整html

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

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