dsbruqxgt820011351 2014-11-13 09:04
浏览 71

Joomla 3.3 AJAX通话

The following code is in my 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:

 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). What am I doing wrong here?

Please show me some directions on that with some relevant sample/example code.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongpo7467 2014-11-14 21:25
    关注
    1. If you're calling 'somefile.php' as your URL that will equate to the current URL you're browsing in Joomla + the file name. It will not map to the path of your file.

      e.g. if you're on the front page of the site i.e. http://example.com the AJAX request will go to
      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.

      Both of these will be wrong as your file is actually somewhere like:

      http://example.com/components/com_mycomponent/view/tmpl/somefile.php

    2. 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.

    3. 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.

    4. Some Joomla 3 htaccess configurations and security extensions (AdminTools) will prevent direct access to php files. If this is the problem, it's probably a sign that you're doing it in an unsafe/incorrect way.

    5. Since you're using Joomla 3.3 read about Using Joomla Ajax Interface on the Joomla Doc's site.

    6. 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.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?