jQuery Ajax simple call

前端 未结 2 356
忘掉有多难
忘掉有多难 2020-12-01 12:08

I\'m trying a basic ajax call. So I\'m hosting the following test php on a test server: http://voicebunny.comeze.com/index.php?numberOfWords=10 This web page is my own t

2条回答
  •  [愿得一人]
    2020-12-01 13:09

    You could also make the ajax call more generic, reusable, so you can call it from different CRUD(create, read, update, delete) tasks for example and treat the success cases from those calls.

    makePostCall = function (url, data) { // here the data and url are not hardcoded anymore
       var json_data = JSON.stringify(data);
    
        return $.ajax({
            type: "POST",
            url: url,
            data: json_data,
            dataType: "json",
            contentType: "application/json;charset=utf-8"
        });
    }
    
    // and here a call example
    makePostCall("index.php?action=READUSERS", {'city' : 'Tokio'})
        .success(function(data){
                   // treat the READUSERS data returned
       })
        .fail(function(sender, message, details){
               alert("Sorry, something went wrong!");
      });
    

提交回复
热议问题