0

I'm trying to make a POST request in JSON to an external domain but I can't access the server's files to modify them.

When I do this request, I get the following error

XMLHttpRequest cannot load https://external.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.ownedwebsite.com' is therefore not allowed access.

Where is the problem?

Here's the code I'm using:

  $(document).ready(function(){
            $("#submit").on('click', function(){
                event.preventDefault();
                $.ajax({
                    url: 'https://external.com',
                    type : "POST",
                    crossDomain: true,
                    dataType : 'json',
                    beforeSend: function (request)
                    {
                    //request.setRequestHeader("name", "value");
                    },
                    data : $("#formCart").serialize(),
                    success : function(result) {
                    alert('POST done.');
                    },
                    error: function(xhr, resp, text) {
                        alert('POST failed.');
                    }
                })
            });
        });

What could I do? All I need to do is to send this POST form data in JSON format.

L. K.
  • 139
  • 2
  • 12
  • Possible duplicate of [jQuery .ajax() POST Request throws 405 (Method Not Allowed) on RESTful WCF](http://stackoverflow.com/questions/17333013/jquery-ajax-post-request-throws-405-method-not-allowed-on-restful-wcf) – Sven Nov 22 '16 at 12:39
  • call the provider of external.net and ask them to add you to their CORS header – madalinivascu Nov 22 '16 at 12:39
  • I need to perform the request without being abled to modify the CORS header etc.. so no, I can't "call the provider" @madalinivascu – L. K. Nov 22 '16 at 12:40
  • does the provider allow you to use this resource, have you looked at their documentation? – madalinivascu Nov 22 '16 at 12:41
  • The provider is just a test website of my company but I would like to do the request without enabling CORS header on server side due to other issues on the server site that make that not possible @madalinivascu – L. K. Nov 22 '16 at 12:43
  • did you try jsonp ,is the test site capable of using jsonp ? – madalinivascu Nov 22 '16 at 12:48
  • It should, the problem is that JSONP doesn't support a POST request – L. K. Nov 22 '16 at 12:52
  • do you really need a post request? there is nothing you can do if you need a post request, except for modifying the test site – madalinivascu Nov 22 '16 at 12:54
  • The post request is needed. There isn't an alternative way at all? – L. K. Nov 22 '16 at 12:57
  • see http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – madalinivascu Nov 22 '16 at 13:00

1 Answers1

0

if you want to get the json data from twitter you have to register for the api key, that means they are ready to serve with json, similar approaches applies with other sites.

if you simply want to grab the page then you could do it with heroku api

$(document).ready(function(){
      var text = 'https://login.yahoo.com/';
      $.ajaxPrefilter( function (options) {
        if (options.crossDomain && jQuery.support.cors) {
          var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
          options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
          //options.url = "http://cors.corsproxy.io/url=" + options.url;
        }
      });

      $.get(
          'https://en.wikipedia.org/wiki/Thomas_Alva_Edison_Memorial_Tower_and_Museum',
          function (response) {

          var res = response;
           $('#yahoo').append(res);


      });

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="yahoo"></div>
  • I need to send a POST request to it, not just get the page. – L. K. Nov 22 '16 at 13:08
  • register for api key so that they will set the header to `allow your site` –  Nov 22 '16 at 13:09
  • otherwise use 'cURL` with this you can login into other website it is an example of `POST` –  Nov 22 '16 at 13:11
  • So I register to the api and make the call to this link and it will work as a mirror site of the external site I wanna query but while allowing me to create a CORS request? @EaBangalore – L. K. Nov 22 '16 at 14:01