簡體   English   中英

將 jquery 代碼轉換為跨瀏覽器 Ajax 請求的原型以獲得最新推文

[英]Converting jquery code to prototype for a cross browser Ajax request in order to obtain Latest Tweets

將 jquery 代碼轉換為跨瀏覽器 Ajax 請求的原型

我的第一篇文章!

我必須獲取我最新的推文,因此必須執行跨瀏覽器請求。 當前的應用程序使用原型,但我對 jquery 有點熟悉。

所以,我從 jquery 開始:

$.ajax('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?', {
  dataType: "jsonp",
  success:function(data,text,xhqr){
    $.each(data, function(i, item) {
      console.log(item.text);
    });
  }
});

我收到警告:

'Resource interpreted as Script but transferred with MIME type application/json.'

但是,我確實可以看到我的最后一條推文。 美好的。

所以,我決定在原型中做同樣的事情,然后嘗試消除警告,如果有的話。 但是,即使嘗試了幾個小時,我也沒有接近。

這是我最初在原型中提出的。 隨后我進行了很多更改/更改,但沒有一個起作用。

new Ajax.Request('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?', {
  contentType: "jsonp",
  onSuccess:function(transport){
    console.log(transport) ;
  }
});

請求成功,但響應文本為 nil / " "。 我在 Firefox 中沒有收到錯誤,但在 Chrome 中,錯誤是:

XMLHttpRequest cannot load http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?. Origin http://localhost:4000 is not allowed by Access-Control-Allow-Origin.
Refused to get unsafe header "X-JSON"

任何幫助將不勝感激。 謝謝你。

感謝 Darin 讓我回到 Dandean 的 JSONP 原型 js。

雖然我一開始沒有提到(問題有點長),但我嘗試過使用來自 Dandean 的 Ajax.JSONRequest (您所指的鏈接)。 該請求不斷失敗,我沒有 go 進一步使用它,因為我假設在原型中完成它也很簡單,比如 jquery。 由於我沒有更多的答案,所以我決定使用 Ajax.JSONRequest 來解決問題。 請求失敗不是由於網關超時。 這是因為請求 url 中有重復的參數回調。

所以,請求 url 原來是

GET (twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&&callback=?&callba‌ck=_prototypeJSONPCallback_0) 

因此,我在沒有回調的情況下定義了我的 url,它按預期執行。 但是,我仍然收到警告:

Resource interpreted as Script but transferred with MIME type application/json

這是等效的原型:

new Ajax.JSONRequest('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1', {
    onSuccess:function(response){
    response.responseJSON.each(function(item){
        console.log(item.text);
    });

你可以看看下面的頁面

Regarding the error [Refused to get unsafe header "X-JSON"], this can happen if your page is under SSL, but the URL referenced in your AJAX call is not also an HTTPS URL.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM