What is AJAX best practice?

patrykf

I am trying to learn javascript best practices and I am a bit confused. I have red that best ajax practice is:

function doSomething(arg1, arg2) {  
    jQuery.ajax({
        var urlink = resourceURL
        url: urlink,
        cache: false,
        data : "testData"+arg1,
        type: "POST"
    }).done(function(data) {
        var obj = jQuery.parseJSON(data);

        updateHtml1(obj);
        updateHtml2(obj);
    });
}

and not this way:

function getSomething(arg1, arg2) { 
    jQuery.ajax({
        var urlink = resourceURL
        url: urlink,
        cache: false,
        data : "testData"+arg1,
        type: "POST",
        success: function(data) {
            var obj = jQuery.parseJSON(data);

            updateHtml1(obj);
            updateHtml2(obj);
        }
    });
}

I am asking which practice is best and why?

Guffa

Either way is fine, it's just a difference in using the success callback or a promise, and in this case there is no difference. If you would want to return the result from the function doSomething then you would use the first method so that you can return the promise, as the done method then can be bound outside the function.

Both examples are overly complicated, and the var urlink = resourceURL is placed inside an object literal, so neither would actually work. You should also specify the dataType in the call, then the data will be parsed automatically.

In the first example you don't need an extra function wrapper.

function doSomething(arg1, arg2) {  
  jQuery.ajax({
    url: resourceURL,
    cache: false,
    data : "testData"+arg1,
    type: "POST",
    dataType: "json"
  }).done(function(data) {
    updateHtml1(data);
    updateHtml2(data);
  });
}

And the second should be:

function getSomething(arg1, arg2) { 
  jQuery.ajax({
    url: resourceURL,
    cache: false,
    data : "testData"+arg1,
    type: "POST",
    dataType: "json",
    success: function(data) {
      updateHtml1(data);
      updateHtml2(data);
    }
  });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

What is the best practice for making an AJAX call in Angular.js?

From Java

What is the best practice for RestController?

From Dev

What is the best practice of using CompositeDisposables

From Dev

What is the best practice for logging in Umbraco?

From Dev

What is the best practice to get Looper?

From Dev

what is best practice? An attribute or an Entity?

From Dev

What is the best practice in RESTful URL?

From Dev

What is the best practice to normalize CSS?

From Dev

what is the best practice to import library?

From Dev

What is the best practice to improve performance?

From Dev

What is the best practice for password encryption?

From Dev

best practice for single page loaded by ajax and seo

From Dev

Multiple Async AJAX Calls Best Practice

From Dev

AJAX request to Laravel resource controller, best practice

From Dev

oop php what is the best practice for database access?

From Dev

What is the best practice for opening channels on GRPC?

From Dev

What is the best practice for using id as primary key?

From Dev

What is the best practice to scale a select/dropdown

From Dev

What is the best practice for lookup values in SQLAlchemy?

From Dev

What is the best practice to consume a pointer returned as an argument

From Dev

What is the best practice for running multiple background tasks

From Java

What is best practice in converting XML to Java object?

From Dev

What is the best practice for saving iPhone video & pictures?

From Dev

What is the Best practice for writing a SQL query in JDBC

From Dev

What is the best practice for powerbi data preparation

From Dev

What is the best practice for Django project structure?

From Dev

What is best practice for flask error handling?

From Dev

What is the best practice for creating payload for a request?

From Java

What is the best practice to skip a test in TestNG?

Related Related

  1. 1

    What is the best practice for making an AJAX call in Angular.js?

  2. 2

    What is the best practice for RestController?

  3. 3

    What is the best practice of using CompositeDisposables

  4. 4

    What is the best practice for logging in Umbraco?

  5. 5

    What is the best practice to get Looper?

  6. 6

    what is best practice? An attribute or an Entity?

  7. 7

    What is the best practice in RESTful URL?

  8. 8

    What is the best practice to normalize CSS?

  9. 9

    what is the best practice to import library?

  10. 10

    What is the best practice to improve performance?

  11. 11

    What is the best practice for password encryption?

  12. 12

    best practice for single page loaded by ajax and seo

  13. 13

    Multiple Async AJAX Calls Best Practice

  14. 14

    AJAX request to Laravel resource controller, best practice

  15. 15

    oop php what is the best practice for database access?

  16. 16

    What is the best practice for opening channels on GRPC?

  17. 17

    What is the best practice for using id as primary key?

  18. 18

    What is the best practice to scale a select/dropdown

  19. 19

    What is the best practice for lookup values in SQLAlchemy?

  20. 20

    What is the best practice to consume a pointer returned as an argument

  21. 21

    What is the best practice for running multiple background tasks

  22. 22

    What is best practice in converting XML to Java object?

  23. 23

    What is the best practice for saving iPhone video & pictures?

  24. 24

    What is the Best practice for writing a SQL query in JDBC

  25. 25

    What is the best practice for powerbi data preparation

  26. 26

    What is the best practice for Django project structure?

  27. 27

    What is best practice for flask error handling?

  28. 28

    What is the best practice for creating payload for a request?

  29. 29

    What is the best practice to skip a test in TestNG?

HotTag

Archive