Javascript jQuery ajax 调用“未找到”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11251240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:08:46  来源:igfitidea点击:

jQuery ajax call "Not Found" error

javascriptjqueryajax

提问by Leo

Hi I have been trying to make a ajax call to a JSP page. Here's the piece of JS function.

嗨,我一直在尝试对 JSP 页面进行 ajax 调用。这是一段 JS 函数。

<script>
$(function(){

  function myAjaxCall() {
     $.ajax({
        type: "post",
        url: "jsp/common/myJavascriptPage.jsp",
        dataType: "text",
        success: 
            function (result) {
               alert("Got the result: " + result);
            },
            error: function (xhr,status,error) {
               alert("Status: " + status);
               alert("Error: " + error);
               alert("xhr: " + xhr.readyState);
            },
            statusCode: {
               404: function() {
                   alert("page not found");
               }
            }
        });
   }
 });
 </script>

I am constantly getting file not found, even though JSP exists in the URL mentioned. Please note that I am calculating the JSP file location relative to that of webapp directory.

即使提到的 URL 中存在 JSP,我也经常找不到文件。请注意,我正在计算相对于 webapp 目录的 JSP 文件位置。

I tried using the normal AJAX calls (without jQuery), but ended up with same error.

我尝试使用普通的 AJAX 调用(没有 jQuery),但最终出现了同样的错误。

Could you please help me understand why is it not able to locate the jsp?

你能帮我理解为什么它无法找到jsp吗?

回答by Ian Bishop

Please note that I am calculating the JSP file location relative to that of webapp directory.

请注意,我正在计算相对于 webapp 目录的 JSP 文件位置。

This is where you're going wrong.

这就是你出错的地方。

AJAX is being executed from the client's web browser, meaning that it's not relative to anything on the server.

AJAX 是从客户端的 Web 浏览器执行的,这意味着它与服务器上的任何内容无关。

If you were to hit that page in a web browser, where you would go?

如果您在网络浏览器中点击该页面,您会去哪里?

That's where you want your request URL to be.

这就是您希望请求 URL 所在的位置。

Edit: I clearly didn't explain this well enough, so that's have another go!

编辑:我显然没有很好地解释这一点,所以再试一次!

Let's say I have a file on my server that's stored like so:

假设我的服务器上有一个文件存储如下:

my_website/src/webapp/jsp/common/myFile.jsp

my_website/src/webapp/jsp/common/myFile.jsp

If I wanted to access this resource through a publicly accessible URL, it would not be the same as the file path above. It might be something like common/myFile.jspor maybe even common/myFile.

如果我想通过可公开访问的 URL访问此资源,则它与上面的文件路径不同。它可能是类似的common/myFile.jsp,甚至可能是common/myFile

If this were the case, then I must use the publicly accessible URLfor an AJAX request to be able to sent to that URL.

如果是这种情况,那么我必须使用可公开访问的 URL才能将 AJAX 请求发送到该 URL。

回答by Logard

It looks like your URL is incomplete. The most likely cause for the error is that you dont have a fully qualified URL.

您的网址似乎不完整。错误的最可能原因是您没有完全限定的 URL。

Try specifying the full path and see if that helps.

尝试指定完整路径,看看是否有帮助。

回答by Solaivel.S

Please check the following two points on the Page Not found error. I also faced the same problem. My case I was calling a json file to load the data. After correcting the below corrections, it started working fine.

请检查页面未找到错误上的以下两点。我也面临同样的问题。我的情况是调用一个 json 文件来加载数据。更正以下更正后,它开始正常工作。

(1) The File path (URL) should be relative to the current page where the script exeutes

(1) 文件路径 (URL) 应相对于脚本执行的当前页面

(2) Add the .json (text/plain) MIME extension in the IIS Server.

(2) 在IIS Server 中添加.json (text/plain) MIME 扩展名。