Javascript-how can I call a function of php for ajax and php?

Source: Internet
Author: User
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 ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.