0

I Have been using pins to get the information of a pin from pinterest.

The following is the script being used:

<script type="text/javascript">
    function getresponse1()
    {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", "https://widgets.pinterest.com/v3/pidgets/pins/info/?pin_ids="+{Pin ID});
    alert(xmlHttp.status);
    var data=xmlHttp.responseText;
    var jsonResponse = JSON.parse(data);
    var pin_url="www.pinterest.com/pin/"+pin_id+"/";
    var page_name=(jsonResponse["data"][0].pinner.full_name);
    alert(page_name);
    }
    </script>

Whenever XMLHttpRequest() method is being invoked the status returned is always 0 and the xmlHttp.responseText is empty.

But when the link is opened in a browser the response is correct and has all the information of the pin.

EDIT:

Tried implementing cross domain too. But yet the status returns 0. New Script:

<script type="text/javascript">
function getresponse1()
{
    var xhr = new XMLHttpRequest();
    var url="https://widgets.pinterest.com/v3/pidgets/pins/info/?pin_ids=308074430730714588";
      if ("withCredentials" in xhr) {
        xhr.open("GET", url, true);    
      } else if (typeof XDomainRequest != "undefined") {

        xhr = new XDomainRequest();
        xhr.open(method, url);

      } else {

        xhr = null;

      }
      alert(xhr.status);
      var data=xhr.responseText;
}
</script>

Please let me know where i'm making mistake. Thanks in advance

Note: I'm using Chrome browser

Sandeep
  • 1
  • 2

2 Answers2

0

This is an general issue, as you try to do an ajax request to a different site (cross domain).

This isn't an new issue at all, I think here it is well explained and this posts provide also some thoughts about possible solutions.

Community
  • 1
  • 1
jpbaxxter
  • 361
  • 4
  • 12
  • Hi, Thanks for your response. I have implemented a similar code for Google plus Posts, which worked perfectly. I'm not well versed with AJAX. I am using simple Javascript though. I have edited the question and added the new code as suggested in the link you provided. Please have a look into it and let me know where I am making the mistake. Thank you – Sandeep Jul 14 '15 at 07:11
0

AJAX is asynchronous, so your data will only be available from some kind of callback.

xhr.onload = function() {
    if (xhr.status >= 200 && xhr.status < 400) {
        var data = JSON.parse(request.responseText);
    }
};
Zack Argyle
  • 8,057
  • 4
  • 29
  • 37