Ajax url path relative path and absolute path problem

When using Ajax, the URL path can be a relative path or an absolute path.


If we refer to a url as a relative path: ./YourAPI/Action, this path will be related to the url we visit.

Assuming the URL of the current request is http://localhost:8080/YourAPI/, the url in this ajax is: http://localhost:8080/YourAPI/Action.

But if the currently requested URL is http://localhost:8080/YourAPI/Index, then the current URL in ajax is: http://localhost:8080/YourAPI/YourAPI/Action, access error.


If we do not strictly control URL routing and need to solve this problem, we need to use absolute paths.

The method is: use the URL at the beginning of Ajax to change it to use an absolute path. For example, in the above example, url:'/YourAPI/Action', this problem will not occur.



When using Ajax, the URL path can be a relative path or an absolute path.


If we refer to a url as a relative path: ./YourAPI/Action, this path will be related to the url we visit.

Assuming the URL of the current request is http://localhost:8080/YourAPI/, the url in this ajax is: http://localhost:8080/YourAPI/Action.

But if the currently requested URL is http://localhost:8080/YourAPI/Index, then the current URL in ajax is: http://localhost:8080/YourAPI/YourAPI/Action, access error.


If we do not strictly control URL routing and need to solve this problem, we need to use absolute paths.

The method is: use the URL at the beginning of Ajax to change it to use an absolute path. For example, in the above example, url:'/YourAPI/Action', this problem will not occur.



Guess you like

Origin blog.csdn.net/qq_27740983/article/details/76125627