Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

28/09/2021, 17:49 javascript - ajax jquery simple get request - Stack Overflow

 
We changed our privacy policy. Read more.

ajax jquery simple get request


Asked
9 years, 7 months ago Active
2 years, 1 month ago Viewed
155k times

I am making this simple get request using jquery ajax:

28 $.ajax({

url: "https://app.asana.com/-/api/0.1/workspaces/",

type: 'GET',

success: function(res) {

console.log(res);
alert(res);

6 }
});

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!

javascript jquery ajax

Share Improve this question edited Oct 26 '18 at 7:20 asked Feb 13 '12 at 22:52
Follow kartikmaji 0xSina
914 7 21 19.1k 29 128 247

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 Answers Active Oldest Votes

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

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

$.ajax({

Join Stack Overflow


url: to learn, share knowledge, and build your career. ,

"https://app.asana.com/-/api/0.1/workspaces/" Sign up
type: 'GET',

dataType: 'json' // added data type


https://stackoverflow.com/questions/9269265/ajax-jquery-simple-get-request 1/3
28/09/2021, 17:49 javascript - ajax jquery simple get request - Stack Overflow
dataType: json , // added data type

success: function(res) {

console.log(res);

alert(res);

});

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

Share Improve this answer Follow answered Feb 13 '12 at 22:57


Stelian Matei
10.9k 2 22 28

It seems to me, this is a cross-domain issue since you're not allowed to make a request to a
different domain.
10
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

Share Improve this answer edited Aug 25 '19 at 3:56 answered Feb 13 '12 at 23:06
Follow viveknaskar Joao Almeida
1,662 1 13 27 912 2 6 17

var dataString = "flag=fetchmediaaudio&id="+id;

3 $.ajax

({

type: "POST",

url: "ajax.php",

data: dataString,

success: function(html)

alert(html);

});

Share Improve this answer edited Dec 5 '13 at 13:20 answered Dec 5 '13 at 12:53
Follow Nunser user3070157
4,494 8 22 35 55 1 2

3 POST and GET is a different HTTP methods, please read some information about HTTP protocol
– bmalets
Apr 3 '20 at 20:44

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.
1
Join Stack
if youOverflow
use to learn, share knowledge, and build your career. Sign up

https://stackoverflow.com/questions/9269265/ajax-jquery-simple-get-request 2/3
28/09/2021, 17:49 javascript - ajax jquery simple get request - Stack Overflow

$.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)

Share Improve this answer edited May 23 '17 at 11:54 answered Apr 2 '13 at 15:04
Follow Community Bot Matthew Fisher
1 1 173 1 15

var settings = {

"async": true,

1 "crossDomain": true,

"url": "<your URL Here>",

"method": "GET",

"headers": {
"content-type": "application/x-www-form-urlencoded"

},
"data": {

"username": "[email protected]",

"password": "12345678"

$.ajax(settings).done(function (response) {

console.log(response);
});

Share Improve this answer Follow answered Jul 6 '18 at 9:07


PK-1825
1,259 13 30

Join Stack Overflow to learn, share knowledge, and build your career. Sign up

https://stackoverflow.com/questions/9269265/ajax-jquery-simple-get-request 3/3

You might also like