Error message: Uncaught TypeError - The function 'send' is not recognized in the Ajax new Request

When I called my ajax function, I encountered the following error: Uncaught TypeError: ajaxGetData.send is not a function Here is my code:

 <button onclick="myFunc()" class="btn btn-default">Click</button>
  <div id="getRes"></div>
 <script>


  function myFunc(){ 

    var ajaxGetData = new Request({
    url: 'ajax.php',
    method: 'post',
    data: {
      'item1':'item 1',                                               
      'item2':'item 2'                                               
      },
    onRequest: function(){
        document.id('getRes').innerHTML = '<div>is Loading...</div>';
        },
    onSuccess: function(responseText){                                                            
    if(responseText){  
        document.id('getRes').innerHTML = responseText; 
    }else{ 
        document.id('getRes').innerHTML = '';       
    }    
   },
    onFailure: function(){}
  });
  ajaxGetData.send();
  }

 </script>

I am facing an issue with my code. Can anyone help me identify the problem? Thank you.

Answer №1

You confused jQuery ajax with javaScript Request.

A revised and functioning version of your code using js Request can be:

function fetchData() {

    var getRequest = new Request('https://api.github.com/repositories?since=364');
    getRequest.method = 'get';
    fetch(getRequest).then(function (response) {
        return response.json();
    }).then(function (jsonData) {
        if (jsonData) {
            document.getElementById('displayResult').innerHTML = jsonData.map(function(element, index) {
                return element.id;
            });
        } else {
            document.id('displayResult').innerHTML = '';
        }
    }).catch(function (error) {
        console.error(error);
    });
}
<button onclick="fetchData()" class="btn btn-default">Click</button>
<div id="displayResult"></div>

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

What is the best way to link options from a select directive with a different array?

Update: the working jsfiddle can be found here: http://jsfiddle.net/robertyoung/jwTU2/9/ I'm in the process of developing a webpage/app using AngularJS. The specific functionality I aim to achieve involves allowing users to add a row to the timecard ...

Disabling a jQuery function on an external page: Step-by-step guide

I have encountered a challenge while trying to load an anchor tag on an external page using a specific method: <a href="page.html#div> This method is meant to link me to another page's particular div. <div name="div"> stuff </> H ...

Using Laravel with Ajax can sometimes result in successful requests, while other times they may fail

I am facing some issues with my code as a junior programmer. Sometimes when I call data using Ajax in laravel 5.7, it is successful and the data is retrieved. However, upon refreshing, sometimes it is incomplete and logs show an error 500 in my PHP. Strang ...

The stick division shows some bouncy behavior when seen on a mobile device

When viewing on mobile, the #main-categories div seems to be jumping instead of smoothly sticking to the top. What could be causing this behavior? Here is the code snippet I have: var s = $("#main-categories"); var pos = s.position(); $(window).scroll(f ...

The JavaScript exec() RegExp method retrieves a single item

Possible Duplicate: Question about regex exec returning only the first match "x1y2z3".replace(/[0-9]/g,"a") This code snippet returns "xayaza" as expected. /[0-9]/g.exec("x1y2z3") However, it only returns an array containing one item: ["1"]. S ...

Using webpack to bundle node_modules into your application

I am facing an issue while trying to load some modules like: moment echarts In my package.json file, I have the following versions specified: "echarts": "^3.1.10" "moment": "^2.14.1" However, I am encountering the errors below: VM2282:1 Uncaught Ref ...

Comparing the functionality of MVC 3's AjaxHelper with Ajax.ActionLink versus Ajax.RouteLink, as well as the differences between Ajax.BeginForm and Ajax

It is my understanding that: Ajax.ActionLink - Creates a link to a specific action within the current controller Ajax.RouteLink - Generates a link based on the RouteData provided to the helper However, after using MVC 3, I have noticed that Ajax.ActionL ...

Tips for achieving comma-separated user input via ngModel and appending it to an array

I am working on an email template that includes a cc option. I want users to be able to add email addresses with commas separating them and then push them to an array called $scope.notifyCtrl.cc. How can I accomplish this using AngularJS 1.5 and above? mai ...

What are some creative ways to reveal a concealed card through animation?

I have a collection of MUI cards where one card remains hidden until the others are expanded. My goal is to add animation to the hidden card so it doesn't abruptly appear. Below is the styling and logic for achieving this: ** Styling ** const useStyl ...

Generating a tree structure using a JavaScript array

Looking to build a tree structure from a given list of data where the paths are represented like: A-->B-->C-->D-->E.. A-->B-->C-->D-->F.. A-->F-->C-->D-->E.. . . . All possible data paths are stored in an array. The de ...

Leveraging partials on their own

I am currently exploring the possibility of loading a partial in linkedin-dustjs without having to load its parent along with it. For instance, consider this partial (login.dust): {>layout/} {<content} <!-- Login Screen --> {/content} Th ...

Discovering latitude and longitude coordinates from a map URL, then enhancing dynamism by utilizing the coordinates as parameters

Hello, I am currently learning Vue.js and have encountered a challenge with embedding Google Maps URLs. Despite my extensive research on Google, I have not been able to find the solution I need. Here is an example of the URL I am working with: "https: ...

String variable representing the name of a React element

As I was reviewing the source code of a React UI library, I stumbled upon an interesting code pattern that I will simplify here: function Test() { let Button = "button"; // ... return <Button>Click me</Button>; } I'm curious about ...

Create an Ajax request function to execute a PHP function, for those just starting out

I came across a JS-Jquery file that almost meets my needs. It currently calls a PHP function when a checkbox is clicked, and now I want to add another checkbox that will call a different PHP function. My initial attempt was to copy the existing function a ...

A guide on extracting data from various HTML elements effectively with JavaScript

I'm searching for a universal technique to extract values from multiple HTML elements. For instance: <div>Experiment</div> <select><option>Experiment</option></select> <input value="Experiment" /> These thr ...

Expanding list item with jQuery

I am facing an issue with the dropdown functionality of my <li> elements. The problem occurs when clicking on the 4th option, as the expander appears on top instead of dropping down beneath the list item. If you check out this jsFiddle, you can see ...

Express JS backend causing CSS file to fail to load in React project

After doing some research on Stack Overflow, I came across a few solutions but none of them seemed to work for my specific situation. I am currently attempting to serve my React website from an Express backend server. Here is the layout of my folders: cli ...

Tips for implementing filters in Angular2 without using the package field in the console

I am currently experiencing an issue with a filter field in my code. The filter works fine when all the package data is present, however, some items do not have a package field. As a result, I need to filter based on the package name but I am encountering ...

Use column formatting for the table's body section

I have written the code below in HTML to create a table. I am looking to apply a specific style to the table body elements in a column that has been assigned a CSS class, while excluding the header columns from this style application. As an example, I hav ...

What is the best way to iterate through elements that come after a specific element?

I'm confident that I will be able to provide an answer to this very soon... Here's a sample code snippet: <script> function ClearAndRunFuncs(element) { //Clears responses for elements AFTER this element with an onchange //Executes the uni ...