如何使用jQuery从外部文件加载数据

时间:2022-06-02 05:58:26

I am attempting to use AJAX in jQuery to load content to a div, however, it is severely failing:

我试图在jQuery中使用AJAX将内容加载到div,但是,它严重失败:

Here's the javascript:

这是javascript:

$(document).ready(function() {
  $('#webdev').hide();
     $("#apply-webdev").click(function() {
        var form = $("#webdev");
        var formContent = form.find("#webdev");
        form.slideToggle();
        $.ajax({
            url: "api.php?do=get_form_webdev",
            cache: false,
            success: function(data) {
                form.html(data.params);
        },
        dataType: "json"
      });
  });
});

And here's the HTML:

这是HTML:

<div class="rbutton"><button title="Apply for position" id="apply-webdev" onclick="load_webdev_form()">&nbsp;Apply&nbsp;</button></div>
    <div id="webdev">
    <fieldset><legend>Apply for position</legend><div style='padding:10px; text-align:center'><img src='/images/load.gif'/></div></fieldset>
    </div>

What am I doing wrong?

我究竟做错了什么?

EDIT

Below is the new code, based on answers given in this thread:

下面是新代码,基于此主题中给出的答案:

$(document).ready(function() {
    $('#webdev').hide();
    $("#apply-webdev").click(function() {
        $("#webdev").slideToggle();
        $("#webdev").load("api.php?do=get_form_webdev");
    });
    $('#webdevcancel').click(function()
        {
            $('#webdev').hide('slow');
        }
    );
    $('#webdevsave').click(function()
        {
        $('#webdev').block({ 
        message: '<h1>Processing...</h1><img src="/images/load.gif" /><br /><br />', 
        css: { border: '3px solid #a00' } 
    });
        }
    );
});

3 个解决方案

#1


The easier way to load content into an element in jQuery is the load method:

在jQuery中将内容加载到元素中的更简单方法是加载方法:

$("#webdev").load("api.php?do=get_form_webdev");

#2


In the success function below, shouldn't you be using form.html(data.params) instead of formContent.html(data.params)?

在下面的成功函数中,您不应该使用form.html(data.params)而不是formContent.html(data.params)吗?

success: function(data) { formContent.html(data.params); },

success:function(data){formContent.html(data.params); },

#3


What do you mean by "it is severely failing"?
I'd tried to set the dataType to html and use the data without the params like form.html(data.);

你是什​​么意思“它严重失败”?我试图将dataType设置为html并使用没有params的数据,如form.html(data。);

#1


The easier way to load content into an element in jQuery is the load method:

在jQuery中将内容加载到元素中的更简单方法是加载方法:

$("#webdev").load("api.php?do=get_form_webdev");

#2


In the success function below, shouldn't you be using form.html(data.params) instead of formContent.html(data.params)?

在下面的成功函数中,您不应该使用form.html(data.params)而不是formContent.html(data.params)吗?

success: function(data) { formContent.html(data.params); },

success:function(data){formContent.html(data.params); },

#3


What do you mean by "it is severely failing"?
I'd tried to set the dataType to html and use the data without the params like form.html(data.);

你是什​​么意思“它严重失败”?我试图将dataType设置为html并使用没有params的数据,如form.html(data。);