douman6245 2013-07-04 23:06
浏览 68
已采纳

为什么当HTML文件在另一个地方时,jquery不会调用PHP?

I have a folder 'js' with the jQuery function and the PHP script. In another folder I have the HTML file. So this is the structure by now:

/server/js/global.js
/server/js/script.php
/server/html/student.html

The JavaScript code is called from HTML and its working fine, but the jQuery post function doesn't call the PHP script.

If I move the HTML file into the 'js' folder it works perfectly and the PHP script is called fine.

Why does this happens?


I mean: What is the path represented on the next path?

$.post('script.php',...);

Is it calling a file from the JavaScript folder or from the HTML folder?

  • 写回答

2条回答 默认 最新

  • doudu2591 2013-07-04 23:16
    关注

    The problem might be that the Javascript file global.js you are calling is using the path that all Javascript files use, which is the one relative to the webpage you are viewing at that moment. Check to see if this is the problem (like a relative path) and make sure the path is absolute instead of relative. So for example in the global.js the part that says:

        $.post(
            'script.php',
            { nameValue: name.value },
    

    change it to:

        $.post(  
            '/path/to/file/script.php',  
            { nameValue: name.value },
    

    As an example it will look like this for your case:

        $.post(  
            '/server/js/script.php',  
            { nameValue: name.value },
    

    This way it will work for in any case that calls global.js out since the absolute path will not depend on where the viewing file is. It also adapts to any place you put your html file so there is no need to edit the file in the future (Assuming the absolute path will always be in the same place).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗