doulan6150 2011-12-01 01:36
浏览 43
已采纳

来自AJAX调用的外部PHP函数

All the strings on my site are pulled from a php language function named lang() inside lang.php. The function works when I use it within the index.php file, ie:

<p><? echo lang('hello-world'); ?></p>

However, if I use elem.load('something.php');, where something.php contains the same above code, nothing gets returned. In fact, if I type:

<p><? echo haslkdfhasdf('hello-world'); ?></p>

I don't even get an error. Now, after reading this Call PHP function in ajax and using header() I still don't get a returned string. My question then is, why doesn't the function work from inside an external php file loaded through AJAX, and what am I missing?

[EDIT] Whoops forgot my AJAX code. Here's what I'm using to load the file:

$('#main-content').load('_foo/views/error.php');

Here is the error.php file:

<p id="ajax-error-message">
<? echo lang('html'); ?>
</p>

Now, I know that the file loads because if I replace the function with "echo 'Hello World';" it displays it properly. And here is the site incase you need it: http://afoobar.com , when you click on HTML5 Sandbox it executes the AJAX.

I'll try your suggestions in the mean time!

  • 写回答

1条回答 默认 最新

  • dongshi1424 2011-12-01 02:02
    关注

    Since I haven't seen your JavaScript code (minus the elem.load() I can't give you a guaranteed solution. I can, however, suggest that you change elem.load() for something like this:

    $.ajax({
        url: 'something.php',
        cache: false,
        success: function(response) {
            // do something
        },
        error: function(xhr, error) {
            try {
                console.debug(xhr); 
                console.debug(error);
            } catch (err) {
                alert(err);
            }
        }
    });
    

    Additionally, you could do practically the same thing using $.ajaxSetup();.

    EDIT

    Keep in mind that you have include (or require) all the needed files within your AJAX call because it is literally making a separate HTTP request.

    // at the top of error.php
    require_once '/path/to/lang.php';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料