2

How do I send a cross-domain POST request via JQuery

<script type="text/javascript">

    $(document).ready(function () {
        // body...
        var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

        var authString = "test:xdfdsTest";
        var authEncBytes = Base64.encode(authString);

        var authkey = "Basic "+authEncBytes;
        console.log(authkey);




        var jsonData = JSON.stringify({
            "start_date":"2016-12-12",
            "end_date":"2016-12-12"
        });



        $.ajax({
             type: "POST",
             url: "https://test.sample.com/get-employee-details.html",
             data: {},
             headers: {
                 "Authorization": authkey
               },
             contentType: "application/json;charset=ISO-8859-1",

             dataType: "json",
             crossDomain: true,
             success: OnSuccess_linechartplot,
             error: OnErrorCall_linechartplot
         });





    });


</script>

If i send to post method in browser network its showing "OPTIONS" then I am getting some warring message

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://sample.test.com/get-employee-details.html. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)."

If check same think in POSTMAN i am getting result.

G Boomanikandan
  • 124
  • 2
  • 12
  • Don't think this duplicate i tried that one but i didnt get result – G Boomanikandan Dec 21 '16 at 09:40
  • See also: http://stackoverflow.com/questions/31276220/cors-header-access-control-allow-origin-missing – Rory McCrossan Dec 21 '16 at 09:40
  • 100% it's a duplicate. Note that the CORS headers need to go on the response from the server, not your JS code. If you don't have access to the server then you will not be able to do what you require through JS and will need to make the request server side instead – Rory McCrossan Dec 21 '16 at 09:42
  • They are using GET method but i need Post method – G Boomanikandan Dec 21 '16 at 09:43
  • So change it to POST...? – Rory McCrossan Dec 21 '16 at 09:43
  • If i change it to POST. It not going to POST method its going only OPTIONS methods – G Boomanikandan Dec 21 '16 at 09:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131139/discussion-between-g-boomanikandan-and-rory-mccrossan). – G Boomanikandan Dec 21 '16 at 09:46
  • That's correct behaviour as it's a cross domain request. See my first comment again - if the endpoint you're calling does not return CORS headers then what you are trying to do is not possible in JS – Rory McCrossan Dec 21 '16 at 10:46
  • ok your telling this one is duplication. but u mention one page know that page i didnt get any answer who are posting that post. He also didnt mention correct answer. – G Boomanikandan Dec 22 '16 at 08:40
  • As I already stated twice, your answer is that what you're trying to do is not possible unless you can amend the response from the server. If you can't do that then you will need to make the request from the server, not JS code – Rory McCrossan Dec 22 '16 at 08:49

0 Answers0