title: Sending form data to restful web service @POST method using jquery and ajax
body: <p>I have looked everywhere for hours and cannot find a solution that will work for me. I have a simple html form that gets 3 form inputs and needs to send the data to the @POST method of a web service. After creation, the web service will show the updated list. Can anyone tell me what I am doing wrong?</p>
  
  <p>poll.html</p>
  
  <pre><code>    &lt;script&gt;
  
      function myFunction()
      {
          data = $(this).serialize(); 
  
          $.ajax({
              url:         'http://localhost:8080/places3/resourcesC/create',
              method:      'POST',
              dataType:    'text',
              data:       data,
              success:        function() { alert("Worked"); },
              error:      function(error) { alert("Error"); }
          });
      }
  
      &lt;/script&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form onsubmit="myFunction()"&gt;
  
          &lt;p&gt;City: &lt;input type="text" name="city" /&gt; &lt;/p&gt;
          &lt;p&gt;POI1: &lt;input type="text" name="poi1" /&gt; &lt;/p&gt;
          &lt;p&gt;POI2: &lt;input type="text" name="poi2" /&gt; &lt;/p&gt;
          &lt;input type="submit" value="Submit" /&gt;
  
  
      &lt;/form&gt;
  </code></pre>
  
  <p>@POST method of web service</p>
  
  <pre><code>@POST
  @Produces({MediaType.TEXT_PLAIN})
  @Path("/create")
  public Response create(@FormParam("city") String city, 
             @FormParam("poi1") String poi1,
          @FormParam("poi2") String poi2) {
  checkContext();
  String msg = null;
  // Require both properties to create.
  if (city == null || poi1 == null || poi2 == null) {
      msg = "Property 'city' or 'poi1' or 'poi2' is missing.\n";
      return Response.status(Response.Status.BAD_REQUEST).
                                         entity(msg).
                                         type(MediaType.TEXT_PLAIN).
                                         build();
  }       
  // Otherwise, create the Place and add it to the collection.
  int id = addPlace(city, poi1, poi2);
  msg = "Place " + id + " created: (city = " + city + " poi1 = " + poi1 + " poi2 = " + poi2 + ").\n";
  return Response.ok(msg, "text/plain").build();
  }
  </code></pre>