Guide on creating an AJAX request for a POST method

I am new to working with AJAX, and I have been trying to use it for a POST function similar to the curl command below:

curl -X POST -H 'Content-type:application/json' --data-binary '{
 "replace-field":{
 "name":"sell-by",
 "type":"date",
 "stored":false }
}' http://localhost:8983/solr/gettingstarted/schema

Based on available resources in SF, I created the function below. However, the data is not being posted to the app, and there are no console errors.

 function sendData() {
    $.ajax({
        url: 'http://localhost:8983/solr/techproducts/schema',
        headers: {
            "Access-Control-Allow-Origin":"*"
        },
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
              "replace-field":{
                     "name":"sell-by",
                     "type":"date",
                     "stored":false
                     }
                }),
       success: function (response, status, xhr) {                         
                    if (status == "success") {
                        alert("sucess");
                    } else {
                        alert(status + ' - ' + xhr.status);
                    }
                },
        // processData: false, // this is optional
        dataType: 'jsonp',
        jsonp: 'json.wrf',
        crossDomain: true,
    });
}

Answer №1

function sendDataToServer() {
    $.ajax({
        url: 'http://localhost:8983/solr/techproducts/schema',
        headers: {
            "Access-Control-Allow-Origin":"*"
        },
        type: 'POST',
        contentType: 'application/json',
        data: {
              "replace-field":{
                     "name":"sell-by",
                     "type":"date",
                     "stored":false
                     }
                },
                success: function (response, status, xhr) {                   

                    if (status == "success") {
                        alert("Data successfully sent!");

                    } else {
                        alert("Error: " + status + ' - ' + xhr.status);
                    }
                },
        // processData: false, // this is optional
        dataType: 'jsonp',
        jsonp: 'json.wrf',
        crossDomain: true,
    });
}

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

Using JavaScript, concatenate text from each line using a specified delimiter, then add this new text to an unordered list element

I am looking to extract text from named spans in an unordered list, combine them with a '|' separating each word within the same line, and append them to another ul. Although my code successfully joins all the words together, I'm struggling ...

Encountered an error while compiling the ./src/App.js file - Module could not

Encountering a challenge while attempting to run my system on the local server. After running npm install, followed by npm run start, I encountered the following error: Failed to compile ./src/app/App.js Module not found: Can't resolve 'assets/ ...

After making a POST request, the `Req.body` is assigned to

This is the JavaScript code I am using: app.use(express.static(__dirname)); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); // support json encoded bodies app.get('/', function(req, res){ res.sendFile(__dirn ...

Events triggered by JQueryUI's .selectable functionality

I am facing a minor issue with the JQuery .selectable function. My goal is to bind events to each tab. I have successfully handled the click event for each tab, but I'm struggling when it comes to selecting two or more tabs. For instance, if I clic ...

Methods for retrieving and persisting the data from a particular row within a table using JavaScript or jQuery

Is there a way to store the selected row values of a table in an array of arrays using JavaScript / jQuery? Check out this JSFiddle I believe that we should listen for the event when the 'Show selected' button is clicked. For instance, in Java ...

Track the loading time of a webpage and the time it takes to render all subelements of

For my project, I need to track page load times for each individual visitor. My idea is to embed a JavaScript snippet into the page in order to achieve this goal. However, the task is more complex than I anticipated because I also need to measure the respo ...

Implementing ASP.net MVC: An empty dropdownlist binding to a model when changed

I have a scenario where I am using two drop down lists on a view. When a selection is made in the first list, I am dynamically populating the second list through an ajax call: $('#NonSelectedCourses').change(function () { console.info(th ...

Ensure that your script is set to execute only once all external content has been loaded successfully

Without diving into the usual question of how to execute a script after a page has finished loading, I have an example set up here (check out this fiddle): $.getScript("http://platform.linkedin.com/in.js").done(function() { alert('hello'); ...

A guide on using FileReader to efficiently read multiple images

I need to be able to select multiple images from one input and read them using File Reader. If I leave it as is, it only gives me one picture. const [image , setImage] = useState(''); const [imageSelected , setImageSelected] = useState(nul ...

Tips for updating a list of orders using keywords entered in a text input field

I have a collection of car objects and an input field that triggers a change event. When the value in the input field is updated, I want to reorganize the order of the cars. For example, if I enter "Tau" in the input box, I want the ford object to be plac ...

What is the best way to showcase an image using Next.js?

I'm having a problem with displaying an image in my main component. The alt attribute is showing up, but the actual image is not appearing on the browser. Can someone please point out what I might be doing wrong? import port from "../public/port. ...

Is there a way to send arguments to a pre-existing Vue JS application?

A Vue application we've developed connects to a Web Service to retrieve data. However, the URL of the web service varies depending on the installation location of the app. Initially, we considered using .env files for configuration, but realized that ...

Using the select method in JavaScript arrays

Are the functionalities of JavaScript and Ruby similar? array.select {|x| x > 3} Could it be done like this instead: array.select(function(x) { if (x > 3) return true}) ...

The tension settings in Chart.JS appear unusual

I recently updated to ChartJS v4.0.1 and noticed a new option called tension for curving the line chart. However, I'm not satisfied with how it looks. The tension option ranges from 0 to 1, and I've experimented with different values to enhance ...

No data returned in cURL JSON response

Attempting to retrieve data from the server using curl, but after authorization, receiving an empty response. The website on this server utilizes AJAX to display content, so initially checking headers and query params sent while interacting via the browser ...

A simple demo showcasing interactive HTML pages using Django and AJAX

In my search for answers, I came across the following helpful posts: How to Reload table data in Django without refreshing the page Tutorial on Django dynamic HTML table refresh with AJAX Despite the insights from these posts, I am still facing chal ...

Adjusting the dimensions of the box while the clock is ticking

I am looking to automatically resize a cube whenever a new value is entered in the input text box. I have found a similar solution that updates the cube size only when a button is clicked. http://jsfiddle.net/EtSf3/3/ document.getElementById('btn&ap ...

"AngularJS Bi-Directional Data Binding Issue: Unexpected 'Undefined

Recently, I've been tackling a project that involves using a directive. Initially, everything was smooth sailing as I had my directive set up to bind to ngModel. However, I hit a roadblock when I needed to bind multiple values. Upon making the necessa ...

Tips for Embedding React into a Specific ID using Hooks on an HTML Document

I am currently diving into the world of React hooks. While I understand class components, Hooks seem to be missing a specific render section. When adding React to a webpage (without using CREATE REACT APP), how do I specify where my hook should run on the ...

Using jQuery.post to select and manipulate the target div displaying Google search results

I am trying to implement a custom form and display the results in a specific target DIV ('results') using ajax. However, I am facing difficulties and unable to figure out what is going wrong. Even after referring to this demo (https://api.jquery ...