$就调用没有传递给servlet时,将返回错误"未发现" [英] $.ajax call not passing to servlet, returns error "Not Found"

查看:163
本文介绍了$就调用没有传递给servlet时,将返回错误"未发现"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用如下Ajax调用来调用一个servlet:

I am trying to call a servlet using ajax call as below:

$.ajax({
  url: 'CheckingAjax',
  type: 'GET',
  data: { field1: "hello", field2 : "hello2"} ,
  contentType: 'application/json; charset=utf-8',
  success: function (response) {
    //your success code
    alert("success");
  },
  error: function (errorThrown) {
    //your error code
    alert("not success :"+errorThrown);
  }                         
});

然而,它进入错误功能并显示警告:

没有成功:找不到

这是怎么造成的,我该怎么解决呢?

How is this caused and how can I solve it?

推荐答案

当您指定相对URL(一个URL不是以计划或 / ),那么它会成为相对于当前请求的URL(在浏览器地址栏中看到URL)。

When you specify a relative URL (an URL not starting with scheme or /), then it will become relative to the current request URL (the URL you see in browser's address bar).

您告诉你的servlet,请访问:

You told that your servlet is available at:

的http://本地主机:8080 / FullcalendarProject / CheckingAjax

想象一下,你的AJAX脚本运行IS通过打开网页:

Imagine that the web page where your ajax script runs is opened via:

的http://本地主机:8080 / FullcalendarProject /页/ some.jsp

和您指定的相对URL URL:CheckingAjax,那么这将是PTED为跨$ P $:

And you specify the relative URL url: "CheckingAjax", then it will be interpreted as:

的http://本地主机:8080 / FullcalendarProject /页/ CheckingAjax

但是,这并不存在。这将因此返回一个HTTP 404找不到网页错误。

But this does not exist. It will thus return a HTTP 404 "Page Not Found" error.

为了得到它的工作,你基本上需要指定使用以下方法之一的网址:

In order to get it to work, you basically need to specify the URL using one of below ways:

  1. URL:HTTP://本地主机:8080 / FullcalendarProject / CheckingAjax

这是不可移植。你需要每次编辑它,你的Web应用程序移动到另一个域。您无法从Web应用程序内部控制这一点。

This is not portable. You'd need to edit it everytime you move the webapp to another domain. You can't control this from inside the webapp.

URL:/ FullcalendarProject / CheckingAjax

这还算不上便携。你需要编辑它每次你改变了上下文路径。您无法从Web应用程序内部控制这一点。

This is also not really portable. You'd need to edit it everytime you change the context path. You can't control this from inside the webapp.

URL:../ CheckingAjax

这其实也是不可移植的,但你完全可以从Web应用程序内部控制这种情况。你需要每次编辑它在你身边JSP移动到另一个文件夹,但如果你走动的JSP你基本上已经处于繁忙状态的编码,所以这可以很容易地在同一时间完成。

This is actually also not portable, although you can fully control this from inside the webapp. You'd need to edit it everytime you move around JSP into another folder, but if you're moving around JSPs you're basically already busy coding, so this could easily be done at the same time.

最好的办法是让JSP EL动态打印当前请求上下文路径。假设JS code被封闭在JSP文件:

Best way would be to let JSP EL dynamically print the current request context path. Assuming that the JS code is enclosed in JSP file:

URL:$ {pageContext.request.contextPath} / CheckingAjax

或者当它封闭在自己的JS文件(很好的做法!),然后创建一个全局变量JS:

Or when it's enclosed in its own JS file (good practice!), then create a global JS variable:

<script>var contextPath = "${pageContext.request.contextPath}";</script>
<script src="yourajax.js"></script>

通过

网​​址:的contextPath +/ CheckingAjax

这篇关于$就调用没有传递给servlet时,将返回错误&QUOT;未发现&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆