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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大