通过javascript

时间:2016-06-05 20:46:43

标签: javascript java rest tomcat google-chrome-extension

我想在javascript中执行http POST请求。

这是Chrome扩展程序background.js文件,我希望将当前标签网址发送到Apache tomcat 8.0上托管的Rest服务

document.addEventListener('DOMContentLoaded', function () {

          chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
          alert(tabs[0].url)
          var http = new XMLHttpRequest();
            var params = tabs[0].url;
            var someValue = "http://localhost:8080/GetSomeRest/webresources/service/";

            http.open("POST", someValue, true);

            http.setRequestHeader("Content-type", "text/plain; charset=utf-8");
            http.setRequestHeader("Content-length", params.length);
            http.setRequestHeader("Connection", "close");

            http.onreadystatechange = function() {
            if(http.readyState == 4 && http.status == 200) {
                alert(http.responseText);
            }
            http.send(params);
        });
      });

My Rest服务工作正常,因为我使用POSTMAN扩展验证了它。为什么我的扩展无法将URL作为参数发送到java模块?

这是我的java代码快照

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vichargrave;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.POST;


@Path("service")
public class ServiceResource {

    @Context
    private UriInfo context;

    public ServiceResource() {
    }


    @POST
    @Consumes("text/plain")
    @Produces("text/plain")
    public String postHandler(String content) {
        System.out.print("gre");
        System.out.println(content);
        return content+"chum"+"bj";
    }

}

任何提示? 如果有人要求清楚,我可以进一步发布我的REST服务的代码以及chrome扩展。感谢

0 个答案:

没有答案