2012-02-13 115 views
10

我使用jQuery AJAX使得这个简单的GET请求:AJAX的jQuery简单的GET请求

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

它返回一个空字符串作为结果。如果我去我的浏览器这个环节,我得到:

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

这是预期的结果。那为什么它不使用ajax? 谢谢!

+1

你尝试过将数据类型:在那里 – 2012-02-13 22:55:44

+0

@KaiQing “JSONP”,这ISN这里根本就没有问题。否则,成功处理程序将不会被调用。此外,示例响应不是JSONP响应。 – Brad 2012-02-13 22:57:29

+0

@PragmaOnce,用Wireshark等数据包检查你的头文件。我怀疑你会发现从浏览器发送的内容和AJAX调用之间的区别。 – Brad 2012-02-13 22:58:39

回答

6

在我看来,这是一个跨领域的问题,因为你不能使请求到不同的域。

你必须为这个问题的解决方案: - 使用代理脚本,在服务器上运行,将转寄您的请求,将处理它发送到浏览器 或者 的反应 - 你正在做请求应该服务有JSONP支持。这是一个跨域技术。您可能想要阅读这个http://en.wikipedia.org/wiki/JSONP

1

您可以对从同一域和同一端口加载的应用程序发出AJAX请求。

除此之外,如果您希望自动对结果进行反序列化,则应该添加dataType JSON

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

1

我认为问题是成功函数中没有数据,因为请求在您的情况下发生了401错误,因此没有成功。

如果使用

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

会有你的401码,我认为(this link是这么说的)

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

$.ajax 
({ 
    type: "POST", 
    url: "ajax.php", 
    data: dataString, 
    success: function(html) 
    { 
    alert(html); 
    } 
});