The status of XMLHttpRequest consistently remains at 0

I want to incorporate the posts JSON from a wordpress.com site into another website, but I keep getting a status of 0 when making the request. This is the function I am using:

var wordPress;

var request = new XMLHttpRequest();
request.open("GET", "https://public-api.wordpress.com/rest/v1.1/sites/mywordpresssite.wordpress.com/posts/", true);

request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
    wordPress = JSON.parse(request.responseText);
    return wordPress;
    }
};

Answer №1

Within the given code snippet, the URL link

it appears that the website {mywordpresssite.wordpress.com} is currently unregistered.

In addition, please refer to the following for more information:

You can try using this code snippet as a reference:-

//create request object   
var httpRequest;

//execute click event 
document.getElementById("ajax_json_Btn").addEventListener('click', makeRequest);

// initiate http request
function makeRequest() 
{
  httpRequest = new XMLHttpRequest();

  if (!httpRequest) {
    alert('Could not create an XMLHTTP instance');
    return false;
  }
  
  httpRequest.onreadystatechange = handleResponse;
  httpRequest.open('GET','https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/7', true);
  httpRequest.send();
}

//handle the http response
function handleResponse() 
{
  if (httpRequest.readyState === XMLHttpRequest.DONE) 
  {
    if (httpRequest.status >= 200 && httpRequest.status < 400) {
      var myObj = JSON.parse(httpRequest.responseText);
      alert(myObj.site_ID);
      // Uncomment the line below to see full response
      // alert(httpRequest.responseText);
    } else {
      alert('There was an issue with the request.');
    }
  }
}

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

Utilizing document.write to switch up the content on the page

Every time I attempt to utilize document.write, it ends up replacing the current HTML page content. For instance, consider this HTML code: <body> <div> <h1>Hello</h1> </div> and this jQuery code: var notif = document. ...

Generate an array that stores the values of objects passed from the parent component

Hi there, I am looking to create a single array in Angular that will store the values of objects coming from the parent component. Here are the objects sent from the parent component console.log('object',o): object {type: "TRA", designator: "EP ...

Accessing data from arrays asynchronously using JavaScript

Update I have included actual code below, in addition to the concept provided earlier. Here is the async array access structure I am trying to implement: for (p = 0; p < myList.length ; p++){ for (k = 0; k < RequestList.length; k++){ i ...

Combining dictionaries within a list by their main keys

I have a list containing multiple dictionaries with specific structures: dict1 = {'first': [{'att1': 'abc', 'att2': '123', 'att3': 'abc123'}, ...

An issue occurred while attempting to serialize or deserialize data with the JSON JavaScriptSerializer. The length of the string exceeds the allowable limit

""""""""""""""""""Issue encountered during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the maximum value set on the maxJsonLength property.","StackTrace":" at System.Web.Script.Serializatio ...

Tips for preventing circular dependencies in JavaScript/TypeScript

How can one effectively avoid circular dependencies? This issue has been encountered in JavaScript, but it can also arise in other programming languages. For instance, there is a module called translationService.ts where upon changing the locale, settings ...

Issues encountered when using Jquery click event for multiple buttons isn't functional

I am using PHP to fetch deliveries from MySQL, and I have a scenario where when the user clicks on the Accept button, I want to display one form, and if they click on Revision, another form should be shown. While I know how to achieve this functionality, i ...

Utilizing Angular's $locationProvider.html5Mode in conjunction with $window parameters

Lately, I encountered some difficulties with Google indexing due to angular routing. After much trial and error, I discovered that using $locationProvider.html5Mode solved the issue. However, a new problem has arisen where $window variables lose their val ...

Exploring the realms of object-oriented programming with JavaScript, jQuery, and AJAX

Here is the code snippet I am working with: var Foo = function(){ this._temp="uh"; }; Foo.prototype._handler=function(data, textStatus){ alert(this._temp); } Foo.prototype.run=function(){ $.ajax({ url: '....', succ ...

In Java, parsing JSON succeeds for numbers but fails for strings

My current task involves developing a parser to handle incoming JSON data that has unpredictable value structures. For instance, a key in the parent JSON can contain an integer, a string, or even another JSON string. While working with the JSON.parse() met ...

Warning from Socket.io: The client has not completed the handshake and should attempt to reconnect

My code has been causing some trouble lately. Even after seeking help from various forums, I am still facing an issue with my nodejs. It keeps disconnecting and sending me a warning message that says: warn: client not handshake client should reconnect. ...

The elements in my code are not displaying as expected

This is my jQuery script: $("#options").popup(null, Settings.DialogOptions) .on("onOk", function(){ Settings.SaveSettings( ); Settings.CloseSettings( ); switch(Settings.GetSetting("displayId")){ case "true": $("#nextId").s ...

Tips on how to properly handle Promises in a constructor function

My Angular Service is currently making http requests, but I am looking to retrieve headers for these requests from a Promise. The current setup involves converting the promise to an Observable: export class SomeService { constructor(private http: HttpCl ...

Loading HTML dynamically

Currently, I am working with node.js, express, and handlebars. When passing JSON data via res.render() to handlebars (hbs), I use {{#each dataJson}} to loop through the data and display it. However, if my JSON file had a large number of entries, loading t ...

The Dropzone feature fails to function properly when displayed on a modal window that is being shown via

After attempting to integrate Dropzone and JavaScript into a Bootstrap Modal called through AJAX, I encountered issues. Due to the high number of records, I avoided placing the Modal in a foreach loop and instead used AJAX to call a controller method upon ...

Troubleshooting inactive CSS hover animation

Greetings! I'm facing an issue with a CSS hover animation. Here are two demos I've created: In the first DEMO, the animation works perfectly. However, in the second DEMO, it doesn't seem to be functioning. On the second demo, there are two ...

Facebook news feeds displaying only the most recent 20 posts

I have my feeds stored in JSON format at this URL, but I am only able to see the last 20 posts. How can I retrieve all of my posts using JSON format? ...

using node and express to route and pass variables to a required module

In my primary index.js file, I have the following code: var express = require('express') require("dotenv").config(); const db = require('./services/db_service').db_connection() const customers = require('./routes/custo ...

How can I send a form without having the page reload using a combination of AJAX, PHP

I am struggling to submit a form without refreshing the page. I have tried using ajax as mentioned in some resources, but it's not working for me. What could be the issue? When I use the following code, everything works fine with PHP: document.getEl ...

Switching an Array to JSON with the help of SwiftyJSON

Seeking a simple solution to convert a 2-dimensional array into JSON format. I am currently using SwiftyJSON but have run into some difficulties (I'm more of a hardware person, haha). Your assistance is greatly appreciated! import UIKit import Swift ...