Home >Web Front-end >Front-end Q&A >What is the url of ajax

What is the url of ajax

WBOY
WBOYOriginal
2022-07-04 16:05:106040browse

The url of ajax refers to the address that initiates the request; there are two ways to write the url path of ajax, one is the complete request path including protocol, host address, port number, and project name, and the other is relative The relative path to the current request address, the syntax is "$.ajax({type:...,url:...,data:...,success:...});".

What is the url of ajax

The operating environment of this article: windows10 system, javascript1.8.5&&html5 version, Dell G3 computer.

What is the url of ajax?

url is a request address initiated by ajax. Look at url:'A.jsp?ids=' id;, ids is one of the request addresses. Parameters, after this request is sent, the ids parameter value is the id you added later.

How to write the url of ajax:

$.ajax({type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){alert( "Data Saved: " + msg );}});

There are two ways to write the url path of ajax, one is The complete request path including protocol, host address, port number, and project name, and the other is a relative path relative to the current request address.

1. Absolute path: the complete request path including protocol name, host address, port, web project name, etc.

For example:

$.ajax({
    url:"http://localhost:8080/webname/test"
});

Benefits: For example, if ajax in the webA project needs to request services in the webB project, the absolute path must be used.

Disadvantages: Using absolute paths requires Paleogeography to change the name of the web project. If the webB project is renamed, the corresponding ajax requests need to be modified.

2. Relative path: No protocol name, host address, port, or web project name is required, only the requested path is required.

Assumption:

Project path: http://localhost:8080/webname

Page path:/webname/index.html (A page),/ webname/test/test.html (B page)

Request path:/request/ajaxtest, request/ajaxtest

1. If the request path starts with the root path, no matter what ajax is in page, the requests are relative to the root path of the server, and the final request path is: http://localhost:8080/request/ajaxtest

For example:

$.ajax({
    url :"/request/ajaxtest"
});

Reason: " /" means that the request is based on the root path of the slave server, that is, it is not a path relative to HTML.

2. If the request does not start with the root path (common), the request path is relative to the path where the html is located.

a. If the request is on page A, the final request path is: http://localhost:8080/webname/request/ajaxtest.

//     /webname/index.html页面
$.ajax({
    url:"request/ajaxtest"
});

Reason: The path corresponding to the index.html page is "/webname/", so following the url under this path is the final request path.

b. If the request is on page B, the final request path is: http://localhost:8080/webname/test/request/ajaxtest.

//     /webname/test/test.html页面
$.ajax({
    url:"request/ajaxtest"
});

Reason: The path corresponding to the test.html page is "/webname/test/", so the url must be followed below the test level.

[Related tutorial recommendations: AJAX video tutorial]

The above is the detailed content of What is the url of ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn