如何在IP地址而不是localhost

时间:2016-09-15 09:57:59

标签: java rest api jsp http

我有一个在PC1上运行的客户端项目(仅具有jsp页面的动态Web项目),以及在PC2上运行的服务器项目(具有带有HTTP方法的java类的动态Web项目)。

我希望能够通过调用PC1上的客户端项目来调用服务器的GETDELETE方法。

我知道我必须在客户端项目中使用 PC2的IP地址,我也尝试过,但它没有正常工作。

让我先附上以下代码:

客户端项目(client.jsp)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>GET user</title>
<script>
function loadResponse()
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open('DELETE','http://PC2_IP_ADDRESS:8080/ServerProject/app/user/service/deleteUser',true);
    //I've written actual PC2 IP address instead of PC2_IP_ADDRESS in my code
    xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv">The response text would come here</div>
<button type ="button" onclick="loadResponse()">submit</button>
</body>
</html>

SERVER PROJECT(UserServices.java)

package com.service.user;
import javax.ws.rs.*;

@Path("/user/service")
public class UserServices {

    @GET
    @Path("/getUser")
    public void getUser()
    {
        System.out.println("Inside GET user method");
    }

    @Path("/deleteUser")
    @DELETE
    public void deleteUser()
    {
        System.out.println("Inside DELETE user method");
    }
}

当我运行client.jsp时,我可以在控制台上看到一些活动(在PC2中),但它只是不打印

  

内部DELETE用户方法

我做错了什么?

控制台活动位于

下方
Sep 15, 2016 4:53:04 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages: 
  com.service.user
Sep 15, 2016 4:53:05 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.service.user.UserServices
Sep 15, 2016 4:53:05 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Sep 15, 2016 4:53:05 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 12:47 PM'
Sep 15, 2016 4:53:05 PM com.sun.jersey.spi.inject.Errors processErrorMessages
WARNING: The following warnings have been detected with resource and/or provider classes:
WARNING: A HTTP GET method, public void com.service.user.UserServices.getUser(), MUST return a non-void type.

1 个答案:

答案 0 :(得分:0)

由于名为cross-site scripting的安全问题,您无法在没有更多配置的情况下从站点A到站点B执行ajax调用。从本质上讲,浏览器默认不会允许它。这是how to fix it

上的Stackoverflow补救措施