Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:20:49+00:00 2026-06-17T18:20:49+00:00

I’m having some problems posting data from a web page, using jQuery, to a

  • 0

I’m having some problems posting data from a web page, using jQuery, to a servlet. While I’m an experienced Java developer, I’m very new to javascript/jQuery/servlets. I’m using Eclipse, Apache Tomcat, and Chrome.

I have an XML file (from 6KB to 30MB in size) that I wish to load into the browser, modify, then post to the servlet.

My HTML has:

<input id="filechooser" type="file" onchange="readFile()">

My JS has:

var file = document.getElementById('filechooser').files[0];
var reader;
reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = loaded;
function loaded(evt){
   var result = evt.target.result;
   var xml = $(result);
   ...
   [make modifications to xml]
}

Some jQuery code that I use in modifying the xml are $(xml).find("NODE").val() and $(xml).find("OTHER_NODE").attr("attribute-name","newValue")

I now need to post that xml to a URL, where it will be used to process some information. In the Chrome console, I can view the content of the xml object:

> xml
  [<!--?xml version="1.0" encoding="ISO-8859-1"?-->,#text,
   <root_element>...</root_element>]

> $(xml)
  [<!--?xml version="1.0" encoding="ISO-8859-1"?-->,#text,
   <root_element>...</root_element>]

> console.dir(xml)
  jQuery.fn.jQuery.init[3]
     0: #comment
     1: #text
     2: root_element
     length: 3
     __proto__: Object[0]

My servlet is empty so far:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   System.out.println("Post");
}

I created a button that executes some javascript. The following two code snippets both post to the server:

$.post("http://localhost:8080/MyWebApp/MyWebAppUrl", xml);

and:

$.ajax({
   type: "POST",
   url: "http://localhost:8080/MyWebApp/MyWebAppUrl",
   data: xml
});

My problem is, I don’t know if I’m sending my XML correctly, or how to properly consume it. What do I need to do to my jQuery code to post it correctly? How do I get it out of my HttpServletRequest? If I can get the xml text as a String, I know exactly how to manipulate it in Java, and get it to do whatever I want.

After 10+ hours searching the web, I still can’t find the answer. I’m sure it’s out there, but I can’t seem to connect the dots.

UPDATE:

epascarello was spot on for posting an XML document. However, I was parsing the document incorrectly.

Notice that I read the file, then stored the result var xml = $(result). The file was read as a text string, and I was converting it to an HTML document.

What I needed to do was var xml = jQuery.parseXML(result). That way, I didn’t have to convert it back to a text string, and tag capitalizing is maintained.

Note that maintaining capitalization is of critical important.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:20:50+00:00Added an answer on June 17, 2026 at 6:20 pm

    Set the content type

    $.ajax({
       type: "POST",
       contentType: "application/xml",  //or text/xml?
       url: "http://localhost:8080/MyWebApp/MyWebAppUrl",
       data: xml
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.