Rest Api,尝试从Rest Server获取数据

时间:2016-10-31 09:03:51

标签: javascript angularjs rest

Rest API与Postman和浏览器一起正常工作。 当通过jsp页面或html页面启动时,它正在点击Rest方法,但响应将变为“0”。

休息JavaScript的客户端 Respose将为“0”,response.data将显示为空白。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title></title>
        <script language="javascript">
        function getdetails() {
        var url = "http://localhost:9090/api/FGame/search1/";
        var client = new XMLHttpRequest();
        client.open("GET", url, true);
        client.send(null);
        if (client.status == 200)
        alert("The request succeeded!\n\nThe response representation was:\n\n" + client.responseText)
        else
        alert("The request did not succeed!\n\nThe response status was: " + client.status + " " + client.statusText + ".");
        alert(client.status);
        }
      </script>
      </head>
      <body>
       <h1>Call Employee Service </h1>
       <table>
       <tr>
           <td>Enter Employee ID :  </td>
           <td><input type="text" id="empno" size="10"/>  <input type="button" value="Get Details" onclick="getdetails()"/>
       </tr>
       <tr>
           <td>Enter Name :  </td>
           <td><input type="text" readonly="true" id="empname" size="20"/> </td>
       </tr>

       <tr>
           <td>Employee Age : </td>
           <td><input type="text" readonly="true" id="age" size="10"/> </td>
       </tr>
       </table>
      </body>
    </html>

Rest API(Dropwizard) 这是Rest Api,我使用了Dropwizard。

@GET
@Timed
@Path("search1")
public String sayHello()
{
    //final User value = dao.findByName(name.get());
    return "{\"id\": 238, \"content\": \"Hello, World!\"}";
}

//我已经和Angular一起使用了同样的错误

<!doctype html>
    <html ng-app="demo">
    <head>
        <title>Hello AngularJS</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
        <script src="js/fGame.js"></script>
    </head>

    <body>
    <div ng-controller="Hello">
        <p>The ID is {{greeting.id}}</p>
        <p>The content is {{greeting.content}}</p>
    </div>
    </body>
    </html>

我也尝试过Angular,但在这种情况下,数据将变为黑色

angular.module('demo', [])
        .controller('Hello', function($scope, $http) {
            $http.get('http://localhost:9090/api/FGame/search1').
            then(function(response) {
                alert($http.status);
                $scope.greeting = response.data;
            });
        });[![Postman Snapshot][1]][1]


  [1]: https://i.stack.imgur.com/1EeXH.png

0 个答案:

没有答案