40

I am making this simple get request using jquery ajax:

$.ajax({
    url: "https://app.asana.com/-/api/0.1/workspaces/",
    type: 'GET',
    success: function(res) {
        console.log(res);
        alert(res);
    }
});

It's returning an empty string as a result. If i go to this link in my browser, i get:

{"status":401,"error":"Not Authorized"}

which is the expected result. So why isn't it working using ajax? thanks!

kartikmaji
  • 946
  • 7
  • 22
0xSina
  • 20,973
  • 34
  • 136
  • 253
  • 1
    have you tried adding dataType: "jsonp" in there – Kai Qing Feb 13 '12 at 22:55
  • @KaiQing, That isn't the problem here at all. Otherwise, the success handler wouldn't be called. Besides, the example response isn't a JSONP response. – Brad Feb 13 '12 at 22:57
  • @PragmaOnce, Check your headers with a packetsniffer, such as Wireshark. I suspect you'll find some differences between what is being sent from the browser, and with the AJAX call. – Brad Feb 13 '12 at 22:58

5 Answers5

46

You can make AJAX requests to applications loaded from the SAME domain and SAME port.

Besides that, you should add dataType JSON if you want the result to be deserialized automatically.

$.ajax({
        url: "https://app.asana.com/-/api/0.1/workspaces/",
        type: 'GET',
        dataType: 'json', // added data type
        success: function(res) {
            console.log(res);
            alert(res);
        }
    });

http://api.jquery.com/jQuery.ajax/

Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
10

It seems to me, this is a cross-domain issue since you're not allowed to make a request to a different domain.

You have to find solutions to this problem: - Use a proxy script, running on your server that will forward your request and will handle the response sending it to the browser Or - The service you're making the request should have JSONP support. This is a cross-domain technique. You might want to read this http://en.wikipedia.org/wiki/JSONP

viveknaskar
  • 2,136
  • 1
  • 20
  • 35
Joao Almeida
  • 972
  • 2
  • 6
  • 19
6
var dataString = "flag=fetchmediaaudio&id="+id;

$.ajax
({
  type: "POST",
  url: "ajax.php",
  data: dataString,
  success: function(html)
  {
     alert(html);
  }
});
Nunser
  • 4,512
  • 8
  • 25
  • 37
user3070157
  • 93
  • 1
  • 2
3
var settings = {
        "async": true,
        "crossDomain": true,
        "url": "<your URL Here>",
        "method": "GET",
        "headers": {
            "content-type": "application/x-www-form-urlencoded"
        },
        "data": {
            "username": "user@company.com",
            "password": "12345678"
        }
    }

    $.ajax(settings).done(function (response) {
        console.log(response);
    });
PK-1825
  • 1,431
  • 19
  • 39
1

i think the problem is that there is no data in the success-function because the request breaks up with an 401 error in your case and thus has no success.

if you use

$.ajax({
        url: "https://app.asana.com/-/api/0.1/workspaces/",
        type: 'GET',
         error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
    alert(thrownError);
  }
    });

there will be your 401 code i think (this link says so)

Community
  • 1
  • 1
Matthew Fisher
  • 173
  • 1
  • 15