Home >Backend Development >PHP Tutorial >javascript - When Thinkphp uses ajax, sometimes an error is reported when using a relative address. What is the reason?
For example:
<code>var root=window.location.origin; $.post(root+"/"+url,data,function(d){ console.log(d); if(d.status==1){ location.reload(); } }) </code>
For example, in the above code, the url has already obtained the relative address using the U method previously.
If I use the URL directly when posting, the entire page will be returned.
After adding root, it can be used normally.
Have you ever encountered a similar situation?
And it only happens like this in some places, it doesn’t happen under normal circumstances.
This page uses Baidu editor, will there be any impact?
Additional explanation:
The above may not be expressed clearly. After I added root, the request address was: root+"/"+url.
When it was not added, the request address was: url (I did not add "at this time" in front. /").
The address of the url is obtained on this page using Thinkphp's U method. Of course, it can be accessed directly and is the correct address; that's why I have such a question.
For example:
<code>var root=window.location.origin; $.post(root+"/"+url,data,function(d){ console.log(d); if(d.status==1){ location.reload(); } }) </code>
For example, in the above code, the url has already obtained the relative address using the U method previously.
If I use the URL directly when posting, the entire page will be returned.
After adding root, it can be used normally.
Have you ever encountered a similar situation?
And it only happens like this in some places, it doesn’t happen under normal circumstances.
This page uses Baidu editor, will there be any impact?
Additional explanation:
The above may not be expressed clearly. After I added root, the request address was: root+"/"+url.
When it was not added, the request address was: url (I did not add "at this time" in front. /").
The address of the url is obtained on this page using Thinkphp's U method. Of course, it can be accessed directly and is the correct address; that's why I have such a question.
The relative address is the address relative to the current browser address bar and has nothing to do with ThinkPHP.
/ is the root address. Directly use / to access from the domain name +/+url. If the address you visit happens to be the root address, it will not be wrong. There will also be problems that cannot be found;
You use window.location. Origin means that you used a relative address. The URL you requested happens to be under the relative address of window.location.origin, so it is correct;
You'd better start your AJAX request path from the root directory and write the full path. This problem It has nothing to do with TP itself. It is your own incorrect understanding of the request address.
base_url
Good practice.