Front end interface requested in several ways (to be continued)

A front-end interface mode requests:

jquery.ajax

usage:

jQuery.ajax([settings])
.ajax $ ({ 
    url: "",     // the request url address 
    Success: function (REQ) {
         // request is successful 
    }, 
    error: function () {
         // request error 
    }, 
    Complete: function () {
         // Request completion 
    } 
});

Common optional parameters:

type

Type: String

Default value: "GET"). Request method ( "POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE may be used, but only partially supported browser.

url

Type: String

Default: the current page address. Transmitting the requested address.

contentType

Type: String

Default value: "application / x-www-form-urlencoded". When the server transmits content information to the encoding type.

The default value for most cases. If you explicitly pass a content-type to $ .ajax () then it must be sent to the server (even if there is no data to send).

Common values ​​of the reference

application/x-www-form-urlencoded:数据被编码为名称/值对。这是标准的编码格式
application/json:消息主体是序列化后的 JSON 字符串
multipart/form-data: 需要在表单中进行文件上传时,就需要使用该格式。常见的媒体格式是上传文件之时使用的
text/plain:数据以纯文本形式(text/json/xml/html)进行编码,其中不含任何控件或格式字符。

async

Type: Boolean

Default value: true. By default setting, all requests are asynchronous request. If you need to send a synchronization request, set this option to false.

Note that the synchronization request will lock the browser, the user must wait for another request to complete before the operation can be performed.

context

Type: Object

This object is used to set the context of Ajax-related callbacks. In other words, let the callback function this point within the object (if you do not set this parameter, then this points to the options parameter passed when calling this AJAX request). DOM element such as a specified context parameter is set and success callback context for the DOM element.

data

Type: String

Sent to the data server. The request is automatically converted to a string format. GET requests will be added after the URL. View processData option to disable this automatic conversion. It must be Key / Value format. If the array, jQuery will automatically correspond to the same name to different values. The {foo: [ "bar1", "bar2"]} is converted into '& foo = bar1 & foo = bar2'

dataType

Type: String

Expected data type returned by the server. If not specified, jQuery will automatically be determined according to intelligent HTTP MIME information packet, such as the XML MIME type was identified as XML. In 1.4, JSON will generate a JavaScript object, and the script will execute this script. Then the server returns the data based on the value resolution, passed to the callback function. Available value:

  • "Xml": returns an XML document, available jQuery process.
  • "Html": Returns the HTML Plain text information; script tag contains will be executed when inserting dom.
  • "Script": return plain text JavaScript code. It does not automatically cached results. Unless the "cache" parameter sets. Note: When remote request (not in the same domain under), all POST requests will be converted to a GET request. (Because of the use of DOM script tag to load)
  • "Json": return JSON data.
  • "Jsonp": JSONP format. When using JSONP form of calling functions, such as "myurl? Callback =?" JQuery automatically replaces the? Correct function name to execute the callback function.
  • "Text": return plain text string

jsonp

Type: String

Rewrite the name of a callback function in jsonp request. This value is used to replace the "callback =?" This URL GET or POST request parameter in the "callback" section, such as {jsonp: 'onJsonPLoad'} will cause "onJsonPLoad =?" To the server.

 

The front end interface request way:

axios

usage:

Use npm: $ npm install Axios

Guess you like

Origin www.cnblogs.com/nanyang520/p/11206290.html