1

I am trying to get the json data from https://twitter.com/i/search/timeline?f=realtime&q=blogger&src=typd

For that i have tried the following cross domain request but i am getting error :/

$(document).ready(function() {
    var contentType = "application/x-www-form-urlencoded; charset=utf-8";

    if (window.XDomainRequest) {
        contentType = "text/plain";
    }

    $.ajax({
        type: 'GET',
        url: 'https://twitter.com/i/search/timeline?f=realtime&q=blogger&src=typd',
        dataType: 'jsonp',
        contentType: contentType,
        success: function(data) {
            alert("Data from Server" + JSON.stringify(data));
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("You can not send Cross Domain AJAX requests: " + errorThrown);
        }
    });
});
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
user3703404
  • 41
  • 1
  • 6
  • What error? can you post error? – MHS Jul 02 '16 at 15:03
  • It's just showing up my message "You can not send Cross Domain AJAX requests" – user3703404 Jul 02 '16 at 15:03
  • Have you verified that the API supports JSON-P? Support for it isn't automatic or guaranteed. The server has to be implemented to use the `callback` for the *Padding* in the response. – Jonathan Lonowski Jul 02 '16 at 15:07
  • @MHS I am using mozilla firefox 46. – user3703404 Jul 02 '16 at 15:08
  • Requests to `https://twitter.com/i/search/timeline` don't appear to support either JSON-P or CORS. Without those, the request won't be possible client-side due to the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) (JSON-P bypasses it, CORS grants exceptions to it, and both require support from the server). The request will likely have to be made from your application's own server-side layer, where the SOP doesn't apply. – Jonathan Lonowski Jul 02 '16 at 15:19
  • @JonathanLonowski So there is no other method than using api? – user3703404 Jul 02 '16 at 15:23

2 Answers2

0

you can't do cross domain requests until the server allows you to do. You should use their API with credentials generaly if they have one.

Another solution is to store the data on your server (which does a curl request to twitter, no CORS problem), and you do ajax calls to your server. But depending on the country, you expose yourself to problems with law.

0

Seems like duplicate of this:

jQuery AJAX cross domain

You'll have to use JSONP.

Edit:

This likely won't work for OP as he's trying to access Twitter. But if you stumble upon this and are trying to use the same domain or two domains s on servers that you control, check that out. If not Pierre is right.

You're likely going to have to find an alternate method of getting that data. You might try writing it in a serverside language and calling that file with AJAX and retrieving the data. To the user it would look the same.

Community
  • 1
  • 1
Michael Heath
  • 174
  • 2
  • 11