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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化