首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XMLHttpRequest js --> java --> js

XMLHttpRequest js --> java --> js
EN

Stack Overflow用户
提问于 2017-02-11 00:10:40
回答 1查看 710关注 0票数 1

我使用XMLHttpRequest将js文件中的一个变量发送到同一项目中的java文件。

我的问题和问题是:我如何知道我的URL?以下是js文件中的代码

代码语言:javascript
复制
xhttp = new XMLHttpRequest();
    var handlerFunction = getReadyStateHandler(xhttp, getValue);
    xhttp.onreadystatechange = handlerFunction;
    xhttp.open("POST",/* Location of my java file */,true);
    xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhttp.send(identMsg);


    function getValue (body) {
        var valueBody = body.getElementByTagName("body")[0];
    }

    function getReadyStateHandler(xhttp, responseXmlHandler) {
        return function(){
            if (xhttp.readyState == 4) {
                if(xhttp.status == 200) {
                    responseXmlHandler(xhttp.responseXML);
                } else {alert("Http error: " +xhttp.status);}
            }
        }
    }

和java代码

代码语言:javascript
复制
public void doPost (HttpServletRequest xhttp, HttpServletResponse res) throws IOException {
     String body = null;
     StringBuilder stringBuilder = new StringBuilder();
     BufferedReader bufferedReader = null;

     try {
        InputStream inputStream = xhttp.getInputStream();
        if (inputStream != null) {
            bufferedReader = xhttp.getReader();
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer,0, bytesRead);
            }

        } else {
            stringBuilder.append("");
        }
} catch(IOException ex) {
    throw ex;
} finally {
    if(bufferedReader != null) {
        try {
                bufferedReader.close();
        }catch (IOException ex2){
            throw ex2;
        }
      }
    }

     body = stringBuilder.toString();
     res.setContentType("application/xml");      
     res.getWriter().write(body);
}

遗漏了什么?

编辑:我需要在js端获取URL。

EN

回答 1

Stack Overflow用户

发布于 2017-02-24 02:48:04

您可以在document.location.href中使用js格式的URL

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42163795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档