[英]Joomla 3.3 AJAX call
The following code is in my view/tmpl/edit.php : 以下代码在我的view / tmpl / edit.php中 :
<script type="text/javascript">
jQuery("#button").click(function(){
jQuery.ajax({
url:'somefile.php',
type:'POST',
data: {action:"1"},
success: function(response) {alert(response);}
});
});
</script>
and in the same folder i have the somefile.php with the following code: 在同一个文件夹中,我具有带有以下代码的somefile.php :
if (isset($_POST['action'])) {
SomeFunction();
}
function SomeFunction()
{
...do something
}
My problem is that when I hit the button it never access the somefile.php although the script is executed (checked with alerts). 我的问题是,当我按下按钮时,尽管脚本已执行(已检查警报),但它从未访问somefile.php 。 What am I doing wrong here? 我在这里做错了什么?
Please show me some directions on that with some relevant sample/example code. 请通过一些相关的示例/示例代码向我显示一些指导。
Thanks. 谢谢。
If you're calling 'somefile.php' as your URL that will equate to the current URL you're browsing in Joomla + the file name. 如果您要调用“ somefile.php”作为您的URL,该URL等于您在Joomla中浏览的当前URL +文件名。 It will not map to the path of your file. 它不会映射到文件的路径。
eg if you're on the front page of the site ie http://example.com
the AJAX request will go to 例如,如果您位于网站的首页上,即http://example.com
则AJAX请求将转到
http://example.com/somefile.php
while on a page at http://example.com/blog/mypost01
it will map to http://example.com/blog/somefile.php
. 而在http://example.com/blog/mypost01
页面上,它将映射到http://example.com/blog/somefile.php
。
Both of these will be wrong as your file is actually somewhere like: 这两个都是错误的,因为您的文件实际上是这样的:
http://example.com/components/com_mycomponent/view/tmpl/somefile.php
First up I would use a proxy like Charles to monitor the full request
and response
, cycle this should give you a clear idea of whats going on and the entire request/redirect/response cycle. 首先,我将使用像Charles这样的代理来监视完整的request
和response
,此周期应该使您清楚了解正在发生的事情以及整个请求/重定向/响应周期。
Make sure you have Error Reporting
turned all the way up to Development
and Joomla DEBUG mode enabled, that way you may see errors rather than a 200
result being returned. 确保您已将Error Reporting
一直转到Development
和Joomla DEBUG模式,这样您可能会看到错误而不是返回200
结果。
Some Joomla 3 htaccess
configurations and security extensions (AdminTools) will prevent direct access to php
files. 一些Joomla 3 htaccess
配置和安全扩展(AdminTools)将阻止直接访问php
文件。 If this is the problem, it's probably a sign that you're doing it in an unsafe/incorrect way. 如果这是问题所在,则可能表明您使用的是不安全/不正确的方式。
Since you're using Joomla 3.3 read about Us ing Joomla Ajax Interface on the Joomla Doc's site . 由于您使用的是Joomla 3.3,请在Joomla Doc的网站上通过Joomla Ajax接口阅读有关我们的信息 。
Finally, as you appear to be building your own component, you could/should be passing the AJAX call via your component and to a method in a suitable controller rather than accessing a PHP file directly. 最后,当您似乎正在构建自己的组件时,您可能/应该通过您的组件并将AJAX调用传递给合适的控制器中的方法,而不是直接访问PHP文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.