If the request is sent by ajax, ajax receives the echo data. if the request is called internally, ajax receives the return data. can I write an if statement, to determine who sent the message, use echo or return? How to write this if statement? If the request is sent by ajax, ajax receives the echo data;
If it is called internally and receives the return data, can I write an if statement to determine who sent the data, and use echo or return?
How to write this if statement?
Reply content:
If the request is sent by ajax, ajax receives the echo data;
If it is called internally and receives the return data, can I write an if statement to determine who sent the data, and use echo or return?
How to write this if statement?
Ajax requests have an "x-requested-with XMLHttpRequest
", You can use it, request. getHeader (" x-requested-with "); if it is null, it is a traditional synchronous request. if it is XMLHttpRequest, it is an Ajax asynchronous request.
However, the above is not completely accurate. you can add a data type format to be returned during the request.
When you have such a requirement, you should consider whether your code structure is faulty.
Yes, but it is not recommended to do so.
Can be judged$_GETOr$_POST
Echo and return are not written in the same function.
There is an IS_AJAX in tp. you can see how it is written.
If you need it, add a field in your returned result set to indicate the type, but there is no need for it during development. I wonder why you need to differentiate between echo and return?
Generally, it does not use an echo and return function.
You can do this
function a(){ return $result;}function 4AJAX(){ echo a();}
Use a () for internal calls and 4 AJAX () for AJAX ().