title: Pure Javascript fetch() method to java Servlet, how do i get and pass data?
body: <p>I facing problems when I'm trying to fetch data from Servlet.
  When I call the servlet by:</p>
  
  <pre><code>function urlFetchRequest(str, body = null, RequestType = 'GET'){
      try
      {
          asyncRequest = new XMLHttpRequest();
          asyncRequest.addEventListener("readystatechange", stateChange, false);
          asyncRequest.open(RequestType, str, true);    //   /Test is url to Servlet!
          asyncRequest.send(body);
      }
      catch(exception)
      {
          alert("Request failed");
      }
  }
  </code></pre>
  
  <p>I get back the information I need from the Servlet, and can use it by: </p>
  
  <pre><code>jsonRes = JSON.parse(asyncRequest.responseText)
  </code></pre>
  
  <p>which is work. My problem is when I need to pass data to the servlet. so I try to do it with fetch() since I have read that I can use JSON objects to the servlet (Again, I cannot use any javascript libraries such as jquery) and I can't use the data that the servlet returns. It getting "undefined".</p>
  
  <p>The javascript scope that I using:</p>
  
  <pre><code> function newFetch(url, options){
      fetch(url, options).then(async (res) =&gt; {
          if(res.ok){
              jsonRes = res.
              if(theSurvey === null) {
                  surveyLength = Object.keys(jsonRes).length;
                  theSurvey = jsonRes;
              }
          }
      });
  }
  </code></pre>
  
  <p>I call it from here:</p>
  
  <pre><code>returnedObject.SurveyLoader = async () =&gt; {
      //urlFetchRequest("/SurveyServlet");
      //await SurveyResults();
      newFetch("/SurveyServlet", {
          method: "GET",
          headers: headers,
      })
  }
  </code></pre>
  
  <p>My servlet is:</p>
  
  <pre><code>@WebServlet(name = "SurveyServlet", urlPatterns = {"/SurveyServlet"} )
  public class SurveyServlet extends HttpServlet {
      public static final Logger LOGGER = Logger.getLogger("SurveyServlet");
      private BufferedReader m_reader;
      private String m_line;
      public String convertWithStream(Map&lt;String, String&gt; map) {
          String mapAsString = map.keySet().stream()
                  .map(key -&gt; '\"' + key + '\"' +  ":" + '\"' + map.get(key) + '\"')
                  .collect(Collectors.joining(", ", "{", "}"));
          return mapAsString;
      }
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      }
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          Map&lt;String, String&gt; SurveyObj = new LinkedHashMap&lt;&gt;();
          m_reader = new BufferedReader(new FileReader(getServletContext().getRealPath("/poll.txt")));
          m_line = m_reader.readLine();
          SurveyObj.put(Integer.toString(0), m_line);
  
          int id = 1;
          while((m_line = m_reader.readLine())!=null) {
              SurveyObj.put(Integer.toString(id), m_line);
              ++id;
          }
          String str = convertWithStream(SurveyObj);
          response.setContentType("application/json");
          response.setCharacterEncoding("UTF-8");
  
  
          //request.setAttribute("ArraySize", id);
         // getServletContext().getRequestDispatcher("ResultServlet").forward(request,response);
          PrintWriter out = response.getWriter();
          out.write(str);
          out.flush();
          out.close();
  
      }
  }
  </code></pre>
  
  <p>My question here is about the fetch method. I know it gets Objects from a REST API. how do I use it to pass and get data from the servlet? how do I use the data I send to the servlet? 
  If it's not possible, how do I use XMLHttpRequest to pass data to the servlet? (no $ use)</p>
  
  <p>Thanks.</p>