How do I send an AJAX request on a different port with jQuery?
You cannot POST
information cross domain, subdomain, or port number. You can however use JSONP if you have access to both the daemon and the requesting site. If data needs to be returned, then the daemon
needs to support a callback
query parameter and return it properly formatted.
Pass the information to the daemon:
$.getJSON('http://domain.com:8080/url/here?callback=?', {
key: 'value',
otherKey: 'otherValue'
}, function(data){
// Handles the callback when the data returns
});
Now just make sure your daemon handles the callback
parameter. For instance, if callback=mycallback
the return from the daemon (the only thing written to the page) should look like this:
For an key/value pairs:
mycallback( {'returnkey':'returnvalue', 'other':'data' });
For an array:
mycallback( [1,2,3] );
If you do not have a JSONP or similar mechanism in place, you cannot communicate cross domain using jQuery.
jQuery Ajax on Different Port
Implementing a JSONP service is really simple, you need only a callback GET parameter and at the end, print a string containing the equivalent to a function call with the JSON data as the argument:
$callback = $_GET["callback"];
$user = $_GET["username"];
if($user == "lazy") {
$response = array("message" => "SUCESS");
} else {
$response = array("message" => "FAIL");
}
echo $callback . "(". json_encode($response) . ");";
Then you can use it with jQuery $.getJSON
:
$.getJSON("jsonpTest.php?callback=?", { username: "lazy"}, function(json){
alert("JSON Data: " + json.message); // SUCCESS
});
Is it possible to specify a port in a ajax call
It doesn't work due the Same origin policy. AJAX requests are allowed only in the same domain, protocol and port.
If you really need to get data from that source, you should look forward to JSONP.
Ajax cross domain on same machine but different port
Two or more documents can be considered in same domain origin, if they have on
- Same Host
- Same Port
- Same Protocol.
In your case port is different so you can not put ajax query directly. Instead you need to specify following header in response.
Access-Control-Allow-Origin: mycompany.com
For more info, check this
Cross Domain Ajax, same server but different port?
You can make SOLR accept JSONP requests, according to http://xplus3.net/2010/09/21/solr-and-jsonp/
Related Topics
Jquery Equivalent of JavaScript's Addeventlistener Method
Difference Between Dom Parentnode and Parentelement
Using Number as "Index" (JSON)
How to Trigger a Link's (Or Any Element'S) Click Event Through JavaScript
How to Smoothly Scroll to an Element in Pure JavaScript
Facebook How to Check If User Has Liked Page and Show Content
Sort an Array of Objects Based on Another Array of Ids
What Does $.When.Apply($, Somearray) Do
How to Check If an Element Is Overlapping Other Elements
How to Remove Spaces from a String Using JavaScript
Extending Built-In Natives in Es6 with Babel
JavaScript Use Variable as Object Name
Angularjs Multiple Filter with Custom Filter Function
Fetch: Reject Promise with JSON Error Object
Componentdidmount Equivalent on a React Function/Hooks Component
JavaScript - How to Get the Url of Script Being Called