Polymer's core-ajax does not support POST requests

Here is the current code structure:

Polymer('my-element', {
  created: function() {
    this.data = {
      name: 'John Doe',
      email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a505552547a5e555f14595557">[email protected]</a>'
    };  
  },
  
  handleResponse: function(e, d) {
    console.log(d.response);
  }
});
<core-ajax
           id="ajax"
           auto
           url="/test"
           method="POST"
           handleAs="json"
           body="{{data}}"
           on-core-response="{{handleResponse"}}>
</core-ajax>

I have set up a server to return the body of the POST message when it is posted to /test.

app.post('/test', function(req, res) {
    res.json(req.body);
}

However, the response I am receiving on the console is as follows:

Object {object Object: ""}

Answer №1

To successfully retrieve all desired data using core ajax with the method post, ensure that the data is sent using the params attribute instead of the body attribute.

Your core ajax tag should appear as follows:

<core-ajax
       id="ajax"
       auto
       url="/test"
       method="POST"
       handleAs="json"
       params="{{data}}"
       on-core-response="{{handleResponse}}">
</core-ajax>

This setup will enable you to receive the data in a standard manner within Node.js:

app.post('/test', function(req, res) {
  res.json(req.body);
}

It's worth noting that while in Node.js parameters are typically associated with GET requests and body with POST requests, core ajax simplifies this by sending both types of data through the params attribute.

Answer №2

It appears that core-ajax is making the AJAX call before accessing this.data in the ready state.

Consider manually initiating the AJAX request by using ajax.go(). You can do this within the created function:

 created: function() {
    this.data = {
       name: 'John Doe',
       email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcb6b3b4b29cb8b3b9f2bfb3b1">[email protected]</a>'
     }; 
    var response = this.$.ajax;
    console.log(response);
    response.go();
},

Here's an example on Plunker

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

Gulp watch isn't doing anything significant

After following a tutorial on using gulp-sass, I have set up a basic configuration along with a .scss file that needs to be compiled into .css. However, when I run gulp watch from the console, the watch task seems to register fine but nothing happens when ...

AngularJS directive facing a callback problem

Having trouble with callbacks while calling a service function This is the function defined in registrationService function checkUserAccess(incentiveLevel, callback, displayRegistrationView) { var url = "..."; httpWrapperService. ...

Despite the presence of data, MongoDB is not returning any values

I am currently working on a code that utilizes $geoNear to find the closest transit stop to a given GPS coordinate. The issue I am facing is that the result sometimes returns undefined, even though I have confirmed that the data exists in the STOPS collect ...

Obtain the customer's Stripe ID when processing a checkout through the portal - "Error: session variable is undefined"

I've been working on this issue for weeks and have spent a considerable amount of time going through various questions on Stack Overflow related to retrieving the customer ID during the Stripe checkout process using Node.js and Mongoose. Unfortunately ...

The act of transmitting data via a timer using JS WebRTC leads to crashes if the page is reloaded before

In one of my server.js files served by a node, I have written the following code snippet: function multiStep(myConnection, data) { var i=0; var myTimer=setInterval(function() { if (i<data.length){ var element=JSON.string ...

Transform a date string into a date entity

Collecting the user's input for the date and saving it as a string variable. The format of the value is Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time) My goal is to convert this string back into a new Date() object in order to make additiona ...

The Promise.all function encountered an error: Uncaught TypeError: #<Promise> is not an iterable object

Currently, I am dealing with the challenge of hitting two APIs and waiting for both responses to return before dispatching my action. Although I am utilizing Promise.all, I am encountering the following error: index.js:51 Uncaught (in promise) TypeErro ...

A guide on extracting/filtering information from JSON files using JavaScript

Is it possible to extract specific data from a JSON file using JavaScript? I have imported a JSON file into my local JavaScript and I am having difficulty in retrieving and displaying certain information. Could someone offer assistance with this? The JS ...

Trying to call the Wia class constructor without using the 'new' keyword will result in a TypeError

I'm having trouble running my code from a JSON file, as it's throwing this error message at me: TypeError: Class constructor Wia cannot be invoked without 'new'at Object. (/home/pi/wia-pi-camera/run-camera.js:3:25) 'use strict&apos ...

Ensure that TypeScript compiled files are set to read-only mode

There is a suggestion on GitHub to implement a feature in tsc that would mark compiled files as readonly. However, it has been deemed not feasible and will not be pursued. As someone who tends to accidentally modify compiled files instead of the source fil ...

JavaScript Ajax request lags significantly in Internet Explorer, while it executes instantly in Firefox

So I have this jQuery .ajax() call set up to retrieve a List<string> of IP addresses from a specific subnet. I'm using a [WebMethod] on an .aspx page to handle the request and ASP.NET's JSON serializer to format the response for my Javascri ...

A step-by-step guide to effectively implement React-Bootstrap Modal

As a newcomer to React, I am excited about utilizing React-Bootstrap Modal. The provided code is functioning perfectly, but I have a specific requirement. Instead of embedding a button within the Trigger component, my goal is to place it in a separate pa ...

Using jQuery, for every button click within a specific class, the content of a div stored in variables should be replaced based on the attribute value

I have a few buttons inside a div. The challenge is to display different content depending on the value attached to the "link" attribute after clicking. When a button is clicked, a function retrieves the value inside the "link" attribute and if it matches ...

Update the CSS for InputLabel

I have a drop-down list that I want to customize. The issue is illustrated below: https://i.sstatic.net/hzVtl.png I'm looking to center the text "choose format" within the field and adjust the font size. return ( <FormControl sx={{ m: 1 ...

Error: Unable to access property 'addEventListener' of null. This issue appears to be related to a Chrome extension

It's been frustrating dealing with this persistent error. I'm in the process of creating my very first chrome extension and for some reason, I just can't seem to pinpoint what's wrong with this particular code snippet: let beginButton = ...

Adjust the dimensions of the embed code to specify a width and height

Looking to integrate Appreplica on my website for embedding content, but struggling with the length of the embedded window. My Tweets seem to extend beyond the page boundaries. ...

Get a new perspective from a different page's refresh

I have a unique situation where I am working on a project that involves two separate pages, each displayed on different computers. What I hope to achieve is that when I click a button on the first page, it triggers a refresh of the second page. Is there a ...

JSON Nesting with Ember.js and Rails

I'm currently developing a simple Ember / Rails application that involves multiple nested relationships. For instance: class Release < ActiveRecord::Base has_many :tracks end Upon accessing my Releases endpoint, I receive JSON data in the follo ...

Eliminate any duplicate objects using JavaScript

Is it possible to remove duplicated objects with the same id using methods other than lodash's _.uniqBy? The id value is always changing, so sometimes I need to remove the object with id:123, but other times it will be a different id. import _ from ...

Display a notification when a user logs in for the very first time

Can someone help me with this specific scenario? My application needs to display an alert when a user logs in from a browser other than Chrome, informing them that the application works best on Chrome. The alert includes two options: "Don't show me a ...