JSLite - Ajax


If you have any questions, you are welcome to communicate in these places, and you are welcome to join the JSLite.io organization team for joint development!

Ajax

Perform Ajax request. The request address can be local or cross-domain, through HTTP access control or through JSONP in supported browsers.

Perform Ajax request.
type: Request method ("GET", "POST")
data: (Default: none) The data sent to the server; if it is a get request, it will automatically be spliced ​​to the url as a parameter. Non-String object
processData (default: true): for non-Get requests. Whether to automatically convert data to string.
dataType: (json, jsonp, xml, html, or text)
contentType: An extra "{key:value}" pair mapped to be sent along with the request
headers: (Default: {}): An extra "{key:value}" pair mapped to be sent along with the request
url : The address to send the request
async: If this parameter is not passed, the default is true (asynchronous request), false for synchronous request
success(cdata): Called after the request is successful. Pass in the returned data and a string containing the success code.
error(status, statusText): Called when a request error occurs. (timeout, parsing error, or status code other than HTTP 2xx).

$.get

$.get(url, function(data, status, xhr){ ... }) ⇒ XMLHttpRequest
$.get( url, [data], [function(data, status, xhr){ ... }], [dataType]) ⇒ XMLHttpRequest

$.get("http://127.0.0.1/api.php?wcj=123", function(cdata) {
    console.log("ok", cdata)
},"json")

$.get("http://127.0.0.1/api.php?wcj=123",{"JSLite":4}, function(cdata) {
    console.log("ok", cdata)
})

$.ajax(GET)

1. Unique to JSLite....

$.ajax("GET", "http://127.0.0.1/api.php", {"wcj":"123","ok":'11'},function(cdata) {    console.log("ok", cdata)
})

2. Universal calling method

$.ajax({
    type:"GET",
    dataType:"json",
    data:{"nike":"a"},
    url:"http://127.0.0.1/api.php",
    success:function(data){
       console.log("success:",data)
    },
    error:function(d){
       console.log("error:",d)
    }
})

$.getJSON

Send an Ajax GET request and parse the returned JSON data. Method supports cross-domain requests.
$.getJSON(url, function(data, status, xhr){ ... })

$.getJSON("http://127.0.0.1/api.php", function(data){  console.log(data)
})

jsonp

JSONP method

$.ajax({
    url: "http://127.0.0.1/api.php?callback",
    dataType: "jsonp",
    success: function(data) {
        console.log(data)
    }
})

$.ajax({
    url: 'http://localhost/api3.php',
    dataType: "jsonp",
    success: function(data) {
        console.log("success:2:",data)
    },
    error:function(d){
       console.log("error:",d)
    }
})

$.post

$.post(url, [data], function(data, status, xhr){ ... }, [dataType])

$.post("http://127.0.0.1/api.php", {"nike":0},function(cdata) {    console.log("ok", cdata)
})

$.ajax( POST)

1. Unique to JSLite....

var data = { "key": "key", "from": "from"}
$.ajax("POST", "http://127.0.0.1/api.php", data,function(data) {    console.log("ok", data)
},"json")

2. Universal calling method

$.ajax({
    type:"POST",
    dataType:"json",
    data:{"nike":"123","kacper":{"go":34,"to":100}},
    url:"http://127.0.0.1/api.php",
    success:function(data){
       console.log("success:",data)
    },
    error:function(d){
       console.log("error:",d)
    }
})
$.ajax({
    type:"POST",
    dataType:"json",
    data:JSON.stringify("{"nike":"123","kacper":{"go":34,"to":100}}"),
    url:"http://127.0.0.1/api.php",
    success:function(data){
       console.log("success:",data)
    },
    error:function(d){
       console.log("error:",d)
    }
})

$.ajax({
    type:"POST",
    dataType:"json",
    data:JSON.stringify({"nike":"a"}),
    url:"http://127.0.0.1/api.php",
    success:function(data){
       console.log("success:",data)
    },
    error:function(d){
       console.log("error:",d)
    }
})

$.ajax({
    type:"POST",
    data:{"nike":"a"},
    url:"http://127.0.0.1/api.php",
    success:function(data){
       console.log("success:",data)
    },
    error:function(d){
       console.log("error:",d)
    }
})

$.ajax({
    type:"POST",
    dataType:"json",
    data:{"nike":"a"},
    url:"http://127.0.0.1/api.php",
    success:function(data){
       console.log("success:",data)
    },
    error:function(d){
       console.log("error:",d)
    },
    headers: {
        "Access-Control-Allow-Origin":"http://pc175.com",
        "Access-Control-Allow-Headers":"X-Requested-With"
    },
    contentType: "application/json"
})

$.ajaxJSONP

is obsolete, use $.ajax instead. This method has no advantages over $.ajax and is not recommended to be used. $.ajaxJSONP(options) ⇒ Simulate the XMLHttpRequest

load

load() method to load data from the server and put the returned data into the selected element.

$(selector).load(URL,data,callback);
Required URL parameter specifies the URL you wish to load.
The optional data parameter specifies the set of query string key/value pairs sent with the request.
The optional callback parameter is the name of the function to be executed after the load() method is completed.

This is the content of the sample file ("demo.txt"):

<h2>JSLite中AJAX的一个方法!</h2>
<p id="demo">这是一个文本文件</p>
// 把文件 "demo.txt" 的内容加载到指定的 <div> 元素中
$("#div1").load("demo.txt");
//把 "demo.txt" 文件中 id="div1" 的元素的内容,加载到指定的 <div> 元素中:
$("#div1").load("demo.txt #p1");