How can I load cross domain html page with jQuery AJAX?











up vote
4
down vote

favorite
2












How can I load cross domain HTML page with jQuery AJAX?



Suppose I want to get a page outside my domain using jQuery AJAX:



$.get('http://www.domain.com/mypage.html', function(data) {
alert(data);
});


I will probably get this error message:




XMLHttpRequest cannot load http://www.domain.com/path/filename. Origin
null is not allowed by Access-Control-Allow-Origin.




we can't load cross domain page using AJAX because of the Same-origin policy.



I could try using 'jsonp' to bypass this restriction:



$.ajax({
type: "GET",
url: url,
dataType: "jsonp",
success: function(data){
console.log(data);
}
});


But what if 'jsonp' is not supported in this site? this could be a problem.



What if I just want to read an external page and parse its HTML?










share|improve this question






















  • Do you have control over the "outside" domain? I ask because if so, look into CORS en.wikipedia.org/wiki/Cross-origin_resource_sharing
    – JeremyWeir
    Sep 7 '14 at 5:23












  • Related: stackoverflow.com/a/17299796/612253
    – Highly Irregular
    Feb 4 '15 at 3:39















up vote
4
down vote

favorite
2












How can I load cross domain HTML page with jQuery AJAX?



Suppose I want to get a page outside my domain using jQuery AJAX:



$.get('http://www.domain.com/mypage.html', function(data) {
alert(data);
});


I will probably get this error message:




XMLHttpRequest cannot load http://www.domain.com/path/filename. Origin
null is not allowed by Access-Control-Allow-Origin.




we can't load cross domain page using AJAX because of the Same-origin policy.



I could try using 'jsonp' to bypass this restriction:



$.ajax({
type: "GET",
url: url,
dataType: "jsonp",
success: function(data){
console.log(data);
}
});


But what if 'jsonp' is not supported in this site? this could be a problem.



What if I just want to read an external page and parse its HTML?










share|improve this question






















  • Do you have control over the "outside" domain? I ask because if so, look into CORS en.wikipedia.org/wiki/Cross-origin_resource_sharing
    – JeremyWeir
    Sep 7 '14 at 5:23












  • Related: stackoverflow.com/a/17299796/612253
    – Highly Irregular
    Feb 4 '15 at 3:39













up vote
4
down vote

favorite
2









up vote
4
down vote

favorite
2






2





How can I load cross domain HTML page with jQuery AJAX?



Suppose I want to get a page outside my domain using jQuery AJAX:



$.get('http://www.domain.com/mypage.html', function(data) {
alert(data);
});


I will probably get this error message:




XMLHttpRequest cannot load http://www.domain.com/path/filename. Origin
null is not allowed by Access-Control-Allow-Origin.




we can't load cross domain page using AJAX because of the Same-origin policy.



I could try using 'jsonp' to bypass this restriction:



$.ajax({
type: "GET",
url: url,
dataType: "jsonp",
success: function(data){
console.log(data);
}
});


But what if 'jsonp' is not supported in this site? this could be a problem.



What if I just want to read an external page and parse its HTML?










share|improve this question













How can I load cross domain HTML page with jQuery AJAX?



Suppose I want to get a page outside my domain using jQuery AJAX:



$.get('http://www.domain.com/mypage.html', function(data) {
alert(data);
});


I will probably get this error message:




XMLHttpRequest cannot load http://www.domain.com/path/filename. Origin
null is not allowed by Access-Control-Allow-Origin.




we can't load cross domain page using AJAX because of the Same-origin policy.



I could try using 'jsonp' to bypass this restriction:



$.ajax({
type: "GET",
url: url,
dataType: "jsonp",
success: function(data){
console.log(data);
}
});


But what if 'jsonp' is not supported in this site? this could be a problem.



What if I just want to read an external page and parse its HTML?







jquery ajax jquery-plugins cross-browser cross-domain






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 7 '14 at 5:16









Ninioe

430156




430156












  • Do you have control over the "outside" domain? I ask because if so, look into CORS en.wikipedia.org/wiki/Cross-origin_resource_sharing
    – JeremyWeir
    Sep 7 '14 at 5:23












  • Related: stackoverflow.com/a/17299796/612253
    – Highly Irregular
    Feb 4 '15 at 3:39


















  • Do you have control over the "outside" domain? I ask because if so, look into CORS en.wikipedia.org/wiki/Cross-origin_resource_sharing
    – JeremyWeir
    Sep 7 '14 at 5:23












  • Related: stackoverflow.com/a/17299796/612253
    – Highly Irregular
    Feb 4 '15 at 3:39
















Do you have control over the "outside" domain? I ask because if so, look into CORS en.wikipedia.org/wiki/Cross-origin_resource_sharing
– JeremyWeir
Sep 7 '14 at 5:23






Do you have control over the "outside" domain? I ask because if so, look into CORS en.wikipedia.org/wiki/Cross-origin_resource_sharing
– JeremyWeir
Sep 7 '14 at 5:23














Related: stackoverflow.com/a/17299796/612253
– Highly Irregular
Feb 4 '15 at 3:39




Related: stackoverflow.com/a/17299796/612253
– Highly Irregular
Feb 4 '15 at 3:39












2 Answers
2






active

oldest

votes

















up vote
0
down vote













I know this is an old post. But, I hope this will help someone else who is looking for the same.



Simply you can't. - same-origin policy or you need to set CORS headers for www.domain.com



But, If you just want to fetch an external page content to your page, there is a workaround you could do:



Create an endpoint in your server to return the HTML content for the given external URL. (because you can't get external content to the browser - same-origin policy)



JS:



var encodedUrl = encodeURIComponent('http://www.domain.com/mypage.html');
$.get('http://www.yourdomain.com/getcontent?url=' + encodedUrl, function(data) {
console.log(data);
});


Easiest way to read from a URL into a string in .NET - may use this to create /getcontent endpoint






share|improve this answer






























    up vote
    -3
    down vote













    You can use 'AJAX Cross Origin' a jQuery plugin to load cross domain HTML page.
    AJAX Cross Origin is a jQuery plugin to allow Cross Origin AJAX requests.
    With this plugin we can easily bypass the Same-origin policy and do cross domain requests.



    It is very simple to use:



       $.ajax({
    crossOrigin: true,
    url: url,
    success: function(data) {
    console.log(data);
    }
    });


    You can read more about it here: http://www.ajax-cross-origin.com/






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25707287%2fhow-can-i-load-cross-domain-html-page-with-jquery-ajax%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      I know this is an old post. But, I hope this will help someone else who is looking for the same.



      Simply you can't. - same-origin policy or you need to set CORS headers for www.domain.com



      But, If you just want to fetch an external page content to your page, there is a workaround you could do:



      Create an endpoint in your server to return the HTML content for the given external URL. (because you can't get external content to the browser - same-origin policy)



      JS:



      var encodedUrl = encodeURIComponent('http://www.domain.com/mypage.html');
      $.get('http://www.yourdomain.com/getcontent?url=' + encodedUrl, function(data) {
      console.log(data);
      });


      Easiest way to read from a URL into a string in .NET - may use this to create /getcontent endpoint






      share|improve this answer



























        up vote
        0
        down vote













        I know this is an old post. But, I hope this will help someone else who is looking for the same.



        Simply you can't. - same-origin policy or you need to set CORS headers for www.domain.com



        But, If you just want to fetch an external page content to your page, there is a workaround you could do:



        Create an endpoint in your server to return the HTML content for the given external URL. (because you can't get external content to the browser - same-origin policy)



        JS:



        var encodedUrl = encodeURIComponent('http://www.domain.com/mypage.html');
        $.get('http://www.yourdomain.com/getcontent?url=' + encodedUrl, function(data) {
        console.log(data);
        });


        Easiest way to read from a URL into a string in .NET - may use this to create /getcontent endpoint






        share|improve this answer

























          up vote
          0
          down vote










          up vote
          0
          down vote









          I know this is an old post. But, I hope this will help someone else who is looking for the same.



          Simply you can't. - same-origin policy or you need to set CORS headers for www.domain.com



          But, If you just want to fetch an external page content to your page, there is a workaround you could do:



          Create an endpoint in your server to return the HTML content for the given external URL. (because you can't get external content to the browser - same-origin policy)



          JS:



          var encodedUrl = encodeURIComponent('http://www.domain.com/mypage.html');
          $.get('http://www.yourdomain.com/getcontent?url=' + encodedUrl, function(data) {
          console.log(data);
          });


          Easiest way to read from a URL into a string in .NET - may use this to create /getcontent endpoint






          share|improve this answer














          I know this is an old post. But, I hope this will help someone else who is looking for the same.



          Simply you can't. - same-origin policy or you need to set CORS headers for www.domain.com



          But, If you just want to fetch an external page content to your page, there is a workaround you could do:



          Create an endpoint in your server to return the HTML content for the given external URL. (because you can't get external content to the browser - same-origin policy)



          JS:



          var encodedUrl = encodeURIComponent('http://www.domain.com/mypage.html');
          $.get('http://www.yourdomain.com/getcontent?url=' + encodedUrl, function(data) {
          console.log(data);
          });


          Easiest way to read from a URL into a string in .NET - may use this to create /getcontent endpoint







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 12:34









          Community

          11




          11










          answered Jul 13 '16 at 17:52









          Safeer Hussain

          83411222




          83411222
























              up vote
              -3
              down vote













              You can use 'AJAX Cross Origin' a jQuery plugin to load cross domain HTML page.
              AJAX Cross Origin is a jQuery plugin to allow Cross Origin AJAX requests.
              With this plugin we can easily bypass the Same-origin policy and do cross domain requests.



              It is very simple to use:



                 $.ajax({
              crossOrigin: true,
              url: url,
              success: function(data) {
              console.log(data);
              }
              });


              You can read more about it here: http://www.ajax-cross-origin.com/






              share|improve this answer

























                up vote
                -3
                down vote













                You can use 'AJAX Cross Origin' a jQuery plugin to load cross domain HTML page.
                AJAX Cross Origin is a jQuery plugin to allow Cross Origin AJAX requests.
                With this plugin we can easily bypass the Same-origin policy and do cross domain requests.



                It is very simple to use:



                   $.ajax({
                crossOrigin: true,
                url: url,
                success: function(data) {
                console.log(data);
                }
                });


                You can read more about it here: http://www.ajax-cross-origin.com/






                share|improve this answer























                  up vote
                  -3
                  down vote










                  up vote
                  -3
                  down vote









                  You can use 'AJAX Cross Origin' a jQuery plugin to load cross domain HTML page.
                  AJAX Cross Origin is a jQuery plugin to allow Cross Origin AJAX requests.
                  With this plugin we can easily bypass the Same-origin policy and do cross domain requests.



                  It is very simple to use:



                     $.ajax({
                  crossOrigin: true,
                  url: url,
                  success: function(data) {
                  console.log(data);
                  }
                  });


                  You can read more about it here: http://www.ajax-cross-origin.com/






                  share|improve this answer












                  You can use 'AJAX Cross Origin' a jQuery plugin to load cross domain HTML page.
                  AJAX Cross Origin is a jQuery plugin to allow Cross Origin AJAX requests.
                  With this plugin we can easily bypass the Same-origin policy and do cross domain requests.



                  It is very simple to use:



                     $.ajax({
                  crossOrigin: true,
                  url: url,
                  success: function(data) {
                  console.log(data);
                  }
                  });


                  You can read more about it here: http://www.ajax-cross-origin.com/







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 7 '14 at 5:16









                  Ninioe

                  430156




                  430156






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25707287%2fhow-can-i-load-cross-domain-html-page-with-jquery-ajax%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini