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条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?