使用jQuery AJAX调用PHP函数 [英] Call PHP Function using jQuery AJAX

查看:68
本文介绍了使用jQuery AJAX调用PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关我的问题的所有主题,但无法解决我的问题. 我想使用jQuery AJAX获得php函数结果.

I've read all the topics about my question but cannot solve my problem. I want to get php function result using jQuery AJAX.

function.php

function.php

function generateCode() {
//code here
}

function generateResult() {
//code here
}

如何使用jQuery AJAX捕获函数结果?我不想更改function.php结构,因为它与其他页面相关.

How can I catch the function result using jQuery AJAX? I don't want to change function.php structure because it related to another pages.

请参考使用jquery $ .ajax调用PHP函数,没有答案可以解决我的问题. 谢谢

Refer to using jquery $.ajax to call a PHP function, no answer can solve my problem. Thank You

推荐答案

您无法从Java脚本中完全停止PHP文件中的函数.客户端(此处为Javascript)只能通过HTTP协议与服务器(此处为PHP)通信. HTTP协议没有文件,函数或编程语言的概念.这意味着您仅限于通过URL或HTTP标头传输信息.因此,您永远无法从Javascript调用PHP函数,您所要做的就是请求返回数据的URL .

You cannot call a function in a PHP file from Javascript, full stop. The client (here: Javascript) can only communicate with the server (here: PHP) through the HTTP protocol. The HTTP protocol has no notion of files, functions or programming languages. This means you're limited to transmitting information via the URL or HTTP headers. As such, you can never call a PHP function from Javascript, all you can do is request a URL which returns data.

因此,通过请求一个URL,例如http://example.com/myscript.php,脚本myscript.php被调用.现在,此脚本必须弄清楚应该做什么并输出一些响应.该脚本实际上可以调用PHP函数,例如:

So, by requesting a URL, say http://example.com/myscript.php, the script myscript.php gets invoked. This script now has to figure out what it should do and output some response. This script can actually call PHP functions, e.g.:

// myscript.php

include 'functions.php'
echo generateCode();

这样,每当您请求URL http://exmaple.com/myscript.php时,您都将获得函数generateCode()的输出.您可能会注意到,这与使用Web浏览器访问URL时的作用相同.这是因为这是使用HTTP协议通过Web服务器运行代码的唯一机制,因此也是Javascript可以调用PHP函数"的唯一方法. Javascript没有秘密后门可以直接调用PHP函数.

As such, whenever you request the URL http://exmaple.com/myscript.php, you will get the output of the function generateCode(). You may notice that this works the same as when you visit a URL with a web browser. That's because this is the only mechanism to run code through a web server using the HTTP protocol and hence the only way Javascript can "call PHP functions". Javascript has no secret backdoor to call PHP functions directly.

希望这会有所帮助.

这篇关于使用jQuery AJAX调用PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆