jQuery ajax调用“Not Found”错误

时间:2022-11-29 13:58:22

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.

即使JSP存在于提到的URL中,我仍然不断找到文件。请注意,我正在计算相对于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吗?

3 个解决方案

#1


13  

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 / web应用/ JSP /普通/ 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.jsp or maybe even common/myFile.

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

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

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

#2


1  

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.

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

#3


0  

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服务器中添加.json(text / plain)MIME扩展。

#1


13  

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 / web应用/ JSP /普通/ 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.jsp or maybe even common/myFile.

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

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

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

#2


1  

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.

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

#3


0  

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服务器中添加.json(text / plain)MIME扩展。