Html 无法从ajax调用简单REST web服务

Html 无法从ajax调用简单REST web服务,html,ajax,json,rest,Html,Ajax,Json,Rest,我正在使用RESTWeb服务开发一个web应用程序。我试图从ajax调用简单的web服务。但我没有得到想要的输出。 我的web服务代码是: @Path("hello") public class Hello { @GET @Path("/first") @Produces("text/html") public String function1(){ return "Something happens"; } } 我的html文件为rest web服务提供

我正在使用RESTWeb服务开发一个web应用程序。我试图从ajax调用简单的web服务。但我没有得到想要的输出。 我的web服务代码是:

@Path("hello")
public class Hello {

   @GET
   @Path("/first")
   @Produces("text/html")
   public String function1(){
   return "Something happens";
   }
}
我的html文件为rest web服务提供了ajax调用,它是:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script>
            function callme(){
            alert("hello");

        $.ajax({
            type: "GET",
            url: "http://localhost:8080/WebApplication4/webresources/hello/first",
            data:"",
            dataType:"text",
            contentType: "text/html",
             success: function(resp){alert("Server says" + resp);},
             error: function(e){ alert("An error has occured");},

         });
       }
      </script> 
    </head>

    <body>

        <form id="form1" method="GET" onsubmit="callme();">
            <input type="text" name="t1">
            <input type="submit" Value="SUBMIT">
        </form>
    </body>
</html>

JSP页面
函数callme(){
警惕(“你好”);
$.ajax({
键入:“获取”,
url:“http://localhost:8080/WebApplication4/webresources/hello/first",
数据:“,
数据类型:“文本”,
contentType:“文本/html”,
成功:函数(resp){alert(“服务器说”+resp);},
错误:函数(e){alert(“发生错误”);},
});
}
当我从浏览器调用web服务时,我的web服务会按预期提供返回值。 我从浏览器中将web服务称为
http://localhost:8080/WebApplication4/webresources/hello/first
然后返回文本
“发生了什么事”
ajax调用在这里出了什么问题? 以及如何在ajax中捕获返回的json对象?
谢谢

这是正确的方法

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        function callme(){
        alert("hello");

    $.ajax({
        type: "GET",
        url: "https://localhost/codeTest.php",
        data:"",
        dataType:"text",
        contentType: "text/html",
         success: function(resp){alert("Server says" + resp);},
         error: function(e){ alert("An error has occured");},

     });
   }
  </script> 
</head>

<body>
    <form id="form1" method="GET">
        <input type="text" name="t1">
        <input type="button" Value="SUBMIT" onclick="callme();">
    </form>
</body>

JSP页面
函数callme(){
警惕(“你好”);
$.ajax({
键入:“获取”,
url:“https://localhost/codeTest.php",
数据:“,
数据类型:“文本”,
contentType:“文本/html”,
成功:函数(resp){alert(“服务器说”+resp);},
错误:函数(e){alert(“发生错误”);},
});
}


ajax调用必须通过html输入类型按钮单击事件进行,而不是表单提交。表单提交事件重新加载页面,它不会等待ajax响应。

这是正确的方法

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        function callme(){
        alert("hello");

    $.ajax({
        type: "GET",
        url: "https://localhost/codeTest.php",
        data:"",
        dataType:"text",
        contentType: "text/html",
         success: function(resp){alert("Server says" + resp);},
         error: function(e){ alert("An error has occured");},

     });
   }
  </script> 
</head>

<body>
    <form id="form1" method="GET">
        <input type="text" name="t1">
        <input type="button" Value="SUBMIT" onclick="callme();">
    </form>
</body>

JSP页面
函数callme(){
警惕(“你好”);
$.ajax({
键入:“获取”,
url:“https://localhost/codeTest.php",
数据:“,
数据类型:“文本”,
contentType:“文本/html”,
成功:函数(resp){alert(“服务器说”+resp);},
错误:函数(e){alert(“发生错误”);},
});
}


ajax调用必须通过html输入类型按钮单击事件进行,而不是表单提交。表单提交事件重新加载页面,它不会等待ajax响应。

此外,您忘记导入jquery.js源文件
$.ajax()
是一个jquery方法。我按照你说的做了,我得到了一些响应,但是我得到了错误中的错误[object]错误:函数(e)看起来,html页面没有问题。正是这个响应导致了这里的错误。确保REST服务返回正确的状态代码和响应。只有当ajax调用返回HTTP错误代码时,才会调用
error
处理程序。只有当HTTP状态代码为200时,才会调用jQuery
success
处理程序。除非它从REST服务接收到200,否则它将无法工作。此外,您忘记导入jquery.js源文件
$.ajax()
是一个jquery方法。我按照你说的做了,我得到了一些响应,但是我得到了错误中的错误[object]错误:函数(e)看起来,html页面没有问题。正是这个响应导致了这里的错误。确保REST服务返回正确的状态代码和响应。只有当ajax调用返回HTTP错误代码时,才会调用
error
处理程序。只有当HTTP状态代码为200时,才会调用jQuery
success
处理程序。除非它从您的REST服务中获得200,否则它将无法工作。