Guide to sending a JSON POST request from JavaScript to an Apache CXF REST Service

Is there a javascript example available for sending a json post request to Apach CXF Rest Service? I would like to utilize this java script specifically for phonegap API.

Answer №1

Here is the code I'm using to make a POST request to the rest service:

$.ajax({
  url: "http://localhost:8080/restService",
  type: "POST",
  data: { data1: "abc", data2: "def"},
  success: function(response){
            alert(response);
              }
});

Below is the snippet for the service:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/restService")
public Collection<Obj> makePostRequest(
        @FormParam("data1") String data1,
        @FormParam("data2") String data2){ //implementation }

Answer №2

Creating a JSON object and sending it via AJAX POST request to a REST service:

var jsonData = {"key1": "value1", "key2": "value2"};

$.ajax({
  url: "http://localhost:8080/restService",
  type: "POST",
  data: jsonData,
  success: function(response){
            alert(response);
              }
});

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/restService")
public Collection<Obj> getPost(
        @RequestBody DataObject jsonData){ //implementation }


DataObject class structure:

Class DataObject {
     private String key1;
     private String key2;

     public void setKey1(String key1){
        this.key1 = key1;
     }

     public String getKey1(){
         return this.key1;
     }

public void setKey2(String key2){
        this.key2 = key2;
     }

     public String getKey2(){
         return this.key2;
     }

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Executing a ternary expression prior to confirming null values

Is it possible to shorten the code below using a ternary expression? if(paginator) { paginator.pageIndex = 1; } I attempted this: paginator.pageIndex = paginator ? 1 : undefined However, I am uncertain if this is the best approach or if there is a mo ...

AngularJS ngRepeat is not complying with the limitTo filter

Currently, I am using ng-repeat to display a list of available supermarket items. These items are fetched in JSON format using $http and stored in $scope.items. The ng-repeat function is working perfectly in displaying all the items. However, my challenge ...

An unexpected error occurred while attempting to establish a connection to a MySQL server using Sequelize: 'LRU is not a constructor'

I have been working on a web application that utilizes boardgame.io's Tic-Tac-Toe tutorial game, with a twist - the results of each match are saved to a MySQL database. The current state of my code involves trying to establish a connection to the data ...

What is the best way to know which API will return the result the fastest?

If we were to make 2 API calls, each taking around 6ms to return JSON data, what would be the sequence in which they provide the resulting data? The official JavaScript documentation mentions using Promise.all to manage multiple API calls. ...

Managing an indefinite amount of values within a PHP script

When submitting the data form, there will be a range of 10 to 100 values to be sent to the PHP file. The quantity of inputs is stored in a JavaScript function variable named count, but I am unsure of how to pass this value to the PHP file. One approach I ...

What circumstances could lead to a timeout while executing sequelize in a Node.js environment?

Within my application built with Node.js version 10.15.0, I utilize the sequelize package (version 4.44.3) to establish a connection to a remote MySQL database. This application operates within a Docker container. However, after prolonged usage, I encounte ...

Setting up the path to partials in Node.js using Express, Handlebars, and Consolidate

I am currently developing a Node.js website using Express.js, Handlebars.js, and Consolidate.js. I want to implement partials for common components in my templates but am facing challenges in making them work across different URLs within the site. Within ...

The submission of the form is not functioning correctly when triggered by JavaScript using a button

My website was designed using a CSS/HTML framework that has been seamlessly integrated into an ASP.NET site. Within a ContentPlaceHolder, I have implemented a basic login form. The unique aspect is that I am utilizing the onclick event of an image to subm ...

Utilize a variable beyond the scope of an ajax success callback

Within a single function, I have a series of timeout functions that execute asynchronously. function myFunction() { var data; setTimeout(function(){ $.ajax({ //My Ajax Stuff success: function(data) { var data = ...

What is the best way to extract function bodies from a string with JavaScript?

I am currently searching for a solution to extract the body of a function declaration by its name from a JavaScript code string within a Node.js environment. Let's assume we have a file named spaghetti.js that can be read into a string. const allJs = ...

Upon the initial submission, the submit function triggers twice in succession, as a result of the interaction

function slideupSubmit (formName) { $(formName).submit(function ( event ) { event.preventDefault(); var target = $(this).attr("action"); var response = $(this).attr("response"); var name = $(this).attr("id"); $. ...

- Tips for preventing requests from being blocked in Node.js

Greetings! I recently crafted a straightforward Node.js server: const express = require('express') const app = express() app.use(express.json()) app.get('/otherget', (req,res) => { ...

I'm thinking about transitioning my current React app to Next.js. Do you think it's a good move?

We have recently transitioned our project from react-redux with firebase authentication and solr DB to next.js. Below is the updated folder structure of our app: --src --actions --assets --components --controllers --firebase --redu ...

Attempting to implement a basic AngularJS integration with Google Maps for a straightforward demonstration

I have a basic Google Maps project that I am trying to transition into an AngularJS project. This is all new to me as I am a beginner in both AngularJS and web development so please bear with me :) The project can be found at https://github.com/eroth/angul ...

React - Error: you have a syntax problem because there is an unexpected token <

I am working on a project using Express, pg, and react. However, I have encountered some issues with React. Here is the directory of my project index.js var express = require('express'); var server = express(); var path = require('path&ap ...

Creating a unique WordPress custom post type with custom fields using the WP-API through REST API

I have successfully created a custom post-type "property" using the REST API by passing title and content. However, I also need to insert values into meta boxes such as CITY, Price, Type, Status, and Country, which are part of the HOUZEZ theme. How can I c ...

Retrieve data from a remote API using Ruby and extract JSON values

I'm having trouble extracting data from last.fm to use in a simple sinatra app. I've figured out how to open the document but am facing issues when trying to retrieve the data in ruby. Here is an example of the API data I want to extract, specifi ...

Tips for accomplishing multiple event triggers and event recollection (benefits that combine features of events and promises)

What I Need Seeking an event system that meets my requirements due to the asynchronous nature of my applications. Specifically, I need the events to have the ability to fire multiple times and for listeners to immediately respond if an event has already b ...

Display an image labeled as "not specified" using multer

I'm attempting to upload a picture using multer, here is my server-side code: const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './uploads/') }, filename: function (req, file, cb) { ...

Tips for creating distinct hover descriptions for each element in an array

My dilemma may not be fully captured by the title I've chosen, but essentially, I am working with a list of names and I want each one to display a unique description when hovered over with a mouse. On the surface, this seems like a simple task, right ...