Making an AJAX request to another server

Asked
Active3 hr before
Viewed126 times

7 Answers

servermakingrequest
90%

As a security measure, AJAX does not allow you to make requests to other domains. Cross Domain Ajax: a Quick Summary discusses several ways to work around the problem. The easiest way is to use your server as a proxy to download remote content:,The advantage to this approach is since it's using the Rewrite Engine, you have more control over the variables you might need to dynamically send to the script.,I have AJAX code where if you request an AJAX call to remote server the request fails:, What do you mean this does not work across the board? Can you give me a scenario? This is the only way to do cross domain "ajax-like" requests, because of the same origin policy... – Dave May 18 '10 at 14:59

The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:

ProxyPass / web - services / http: //third-party.com/web-services/
load more v
88%

Contact the 3rd party site, and ask if they could include you in their CORS policy, if not then ,Make sure there's no JSONP or similar "public access" point to use, if not then,If so how can I overcome this? ideally, JSONP would be a desired option but the data is not in JSON format, in my case it's XML., Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

If I run the following code from www.mysite.com:

var link = "https://www.myothersite.com/jobs-feed.xml";
$.ajax({
   type: "GET",
   url: link,
   dataType: "xml",
   success: function(text) {
      alert(text);
   }
})
load more v
72%

There are so many options in different ways to call Ajax in JavaScript that can improve user experiences drastically like submitting data to the server, checking the username, creating autocomplete form, voting, and rating, creating chat rooms, etc.,This article covers the information about the latest list of various options to make AJAX calls. To keep it simple, let’s focus on what they are, there some pros and cons of each option.,Love to read and write blogs on PHP and JavaScript Frameworks. You can also read my articles from https://www.onlineinterviewquestions.com/,If you have any questions or suggestions regarding this or if you think I should add, correct or remove, feel free to comment.

Performing GET request using XHR

const Http = new XMLHttpRequest();
const url = 'http://yourdomain.com/';
Http.open("GET", url);
Http.send();
Http.onreadystatechange = (e) => {
   console.log(Http.responseText)
}
load more v
65%

execute other scripts while waiting for server response,The XMLHttpRequest object is used to request data from a server.,To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:,By sending asynchronously, the JavaScript does not have to wait for the server response, but can instead:

Send a Request To a Server

To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:

XMLHttpRequest
load more v
75%

In its first form, this function performs an Ajax request using the url parameter and the options specified in settings. In the second form, the URL is specified in the settings parameter, or can be omitted, in which case the request is made to the current page.,The url parameter is a string containing the URL you want to reach with the Ajax call, while settings is an object literal containing the configuration for the Ajax request.,To have an even better understanding of the potential of this function, I encourage you to play with the code samples, and to try to modify the code to use some other options accepted by the settings parameter.,In this section, we’ll see this function and some of its options in action.

The jQuery $.ajax() function is used to perform an asynchronous HTTP request. It was added to the library a long time ago, existing since version 1.0. The $.ajax() function is what every function discussed in the previously mentioned article calls behind the scene using a preset configuration. The signatures of this function are shown below:

$.ajax(url[, settings])
$.ajax([settings])
load more v
40%

This article guides you through the AJAX basics and gives you some simple hands-on examples to get you started.,Make requests to the server without reloading the page,Step 1 – How to make an HTTP request,When you sent the request, you provided the name of a JavaScript function to handle the response:

// Old compatibility code, no longer needed.
if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
   httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 6 and older
   httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
load more v
22%

An object of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:, settings Type: PlainObject A set of key/value pairs that configure the Ajax request. All settings are optional. A default can be set for any option with $.ajaxSetup(). See jQuery.ajax( settings ) below for a complete list of all settings. , crossDomain (default: false for same-domain requests, true for cross-domain requests) Type: Boolean If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5) ,This example, using no options, loads the contents of the current page, but does nothing with the result. To use the result, you can implement one of the callback functions.

$.ajax({
         accepts: {
            mycustomtype: 'application/x-some-custom-type'
         }, // Instructions for how to deserialize a `mycustomtype`  converters: {    'text mycustomtype': function(result) {      // Do Stuff      return newresult;    }  },   // Expect a `mycustomtype` back from server  dataType: 'mycustomtype'});
load more v

Other "server-making" queries related to "Making an AJAX request to another server"