Home  >  Article  >  Backend Development  >  javascript - When Thinkphp uses ajax, sometimes an error is reported when using a relative address. What is the reason?

javascript - When Thinkphp uses ajax, sometimes an error is reported when using a relative address. What is the reason?

WBOY
WBOYOriginal
2016-08-18 09:16:151118browse

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.

Reply content:

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_urlGood practice.

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