Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JQuery Ajax Call to Servlet

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a button that should do an Ajax call to a servlet in order to create/write a Textfile in a certain Directory. How do i transfer the variable "text" to the servlet and does the Ajax call look right?

Posts: 67424
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Two things about your Ajax call:
  • You've hard-coded the URL to use localhost. That means it cannot work anywhere else but your local machine. You should be using a URL that works everywhere. In order to help you figure out what that is, we need some more context. Since you mention Servlets, I assume that this is a Java web application in which the view is provided by a JSP in the same web app? If so, see below. If not, tell us what you've got going on.
  • The data is malformed. It cannot just be a string. See the description of the expected data format in the jQuery documentation for the $.ajax function.

  • In a Java web application, URLs into the same app should be server-relative, starting with the context path. See the JspFaq on resource URLs to learn how to create such URLs in JSPs.
      Bookmark Topic Watch Topic
    • New Topic