0

Need a cross domain post javascript format that return the search value. The total thing need to be done without using any external library and only with javascript.

Example: My server domain1.com Want to post data to domain2.com/searchTableInfo1 with json data like {data1:'info1', data2:'info2', data3:'info3', data4:'info4', ......} (like searching a table with some params) in return I want to get the search result in json too.

M.A.K. Ripon
  • 2,070
  • 3
  • 29
  • 47
  • 2
    the server needs to allow this through the use of CORS headers - then you would either use `fetch` or `XMLHttpRequest` to make a `POST` request with the data, and process the response appropriately depending on which of the two vanilla JS AJAX methods you choose to use – Jaromanda X Mar 15 '17 at 04:39
  • [Duplicate](http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery)? – Isaiah Mar 15 '17 at 05:35
  • 1
    Thanks Jaromanda X for your reply but can give some example or in brief. – M.A.K. Ripon Mar 15 '17 at 06:31
  • 2
    Isaiah that post is only how to make Ajax call without using JQuery. I'm not talking about Ajax call or JQuery here. – M.A.K. Ripon Mar 15 '17 at 06:33
  • Please be more clear about what you are trying to do. cross domain post either means an ajax request *to* a server or are you trying to handle the post request *on* the server *in* javascript? – Isaiah Mar 15 '17 at 19:48
  • 1
    Ok Isaiah, I have given some more clear example now I think this might help – M.A.K. Ripon Mar 18 '17 at 08:42

2 Answers2

1

You need add allow origins in your server part and set some header for a front end part here there are example in details https://www.moxio.com/blog/12/how-to-make-a-cross-domain-request-in-javascript-using-cors

1

Javascript is client-side, so it cannot directly access variables sent to a server through something like a post request.

If you were to do a post request to a server, which can be done in vanilla JS, you have some options for retrieving the values through the server and returning JSON that way.

For example (this comes from JS on domain1.com and is an asynchronous cross-domain request to domain2.com):

var http = new XMLHttpRequest();
var url = "domain2.com/searchTableInfo1";
var params = JSON.stringify(myJSONobj);
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);//the JSON string from server will show here
    }
}
http.send(params);

Depending on server-side language on domain2.com it could look something like this (example is in PHP, but you could be running Node.js, ASP.Net, and various other options):

<?php
    $data = json_decode($_POST['myJSONobj']);

    //handle data processing here

    $jsonToReturn = array("Enter processed data here");
    echo json_encode($jsonToReturn);
?>

Using a post request however will require some server-side processing of the data on domain2.com (non-JS -- unless you are in Node of course!). If this is not possible you can attempt to do a get asynchronous request by changing some of the vars in the JS above and it will give domain1.com the same cross-domain access, but you must understand the scope of Javascript and what it has access to as a client-side language.

Please let me know if you have more questions.

Isaiah
  • 484
  • 5
  • 16