简体   繁体   中英

Use $.get() inside success: function(data) {… in $.ajax().?

Can I use $.get() or $.ajax() inside of success: function(data) {... in $.ajax() ?

Like:

$.ajax({
    url: 'ajax/test1.html',
    success: function (data) {
        $('.result').html(data);
        $.get("test2.php", function (data) {
            alert("Data Loaded: " + data);
        });
    }
});

Or

$.ajax({
    url: 'ajax/test1.html',
    success: function (data) {
        $('.result').html(data);
        $.ajax({
            url: 'ajax/test2.html',
            success: function (data) {
                $('.result').html(data);
                alert('Load was performed.');
            }
        });
  }
});

Does this work correctly? Which of those is best? If it does not work, how would I do it correctly?

Yes, it's possible to do both.

The $.get() is equivalent to:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

See in http://api.jquery.com/jQuery.get/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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