我正在尝试进行$.ajax调用并将一些数据发送到php文件,我的php文件位于
组件的文件夹和webroot文件夹中的js文件,如何使$.ajax的url部分指向正确的php文件,以便从JS文件获得php文件,我需要执行以下操作
../../../src/Controller/Component/AuthComponent.php
但是当我在$.ajax调用中做同样的事情时,就像
$.ajax({
type: 'POST',
url: '../../../src/Controller/Component/AuthComponent.php' ,
data: "accessToken" + access_token,
dataType: 'json',
complete: function (response) {
console.log("data coming back from Auth.php" +response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});我只得到
找不到http://localhost:8765/src/Controller/Component/AuthComponent.php- 404
我怎样才能建这个网址?
发布于 2018-07-09 09:32:36
如果您正在使用一个框架或如果您已经构建路由自己,所有其他网页的网站。然后,与这些类似,您必须为添加到AuthComponent.php中的方法()创建(分配)一个新的路由,例如:
Route: http://localhost:8765/authcomponent/method_name
Pointing to: /src/Controller/Component/AuthComponent.php::methodName()然后,最后的JavaScript代码如下所示:
$.ajax({
type: 'POST',
url: 'http://localhost:8765/authcomponent/method_name' ,
data: "accessToken" + access_token,
dataType: 'json',
complete: function (response) {
console.log("data coming back from methodName() of AuthComponent.php file" +response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
} });
发布于 2018-07-09 09:29:44
我宁愿传递完整的URL (如果有任何可能,您可以将您的域传递给JS代码)。有时,如果您正在编译代码,那么在运行代码时,实际的相对路径可能与目录的顺序不一样(例如,JS可能是从站点dir中的另一个文件夹中的单个位置运行)。
发布于 2018-07-09 09:33:54
您不能使用绝对路径作为url,例如,如果您的文档根是一个名为“public_html”的目录,我的意思是指向该文件夹的站点名,以及您的文件AuthComponent.php放置的内部,您的url是sitename/AuthComponent.php将调用您的文件。
https://stackoverflow.com/questions/51242132
复制相似问题