2015-06-26 199 views
0

http://www.imobilus.co.uk/json%20request/index.html - 真人版JSON請求的Safari瀏覽器,但不是鉻工作/火狐

https://jsfiddle.net/5p6h3rtt/

不能工作了這一點,在所有的,相當新的jQuery

運行完美的探險之旅,但對鉻空白和Firefox

$(document) .ready(function() { 

$.ajax({ 
    type: 'GET', 
    url: 'http://www.reed.co.uk/api/1.0/search?keywords=imobilus%20jobs', 
    beforeSend: function(xhr) { 
xhr.setRequestHeader("Authorization", "Basic " + btoa(' b110030d-7491-48fe-9354-05c0ec0181d7' + ":" + '')); 
}, 
    data: { username: 'b110030d-7491-48fe-9354-05c0ec0181d7', password:'' }, 
    dataType: 'json', 
    success: function (data) { 
     console.log(data.results); 

     if(data.results.length > 0) { 
      var numberOfResults = data.results.length; 
      $('.container').append('<div>Found '+numberOfResults+' results</div>'); 
      $.each(data.results, function(index, element) { 
       if(index < 100) { 
        $('.container').append('<div class="my-class">'+element.locationName+'<div><div>'+element.jobDescription+'</div>'); 
       } 
      }); 
     } else { 
      $('.container').append('<div class="my-class">Sorry nothing found</div>'); 
     } 


    } 
}); 

}); 

編輯:

已經有很多年了,現在已經差不多了,現在它可以在Chrome和Firefox上正常工作,但在IE/Edge上它根本不會顯示,並且在Safari上瀏覽器要求進行身份驗證。

$(document) .ready(function() { 
$.ajax({ 

dataType: 'jsonp', 
type: 'GET', 
url: 'http://b110030d-7491-48fe-9354-05c0ec0181d7:@www.reed.co.uk/api/1.0/search?keywords=imobilus%20jobs', 
beforeSend: function(xhr) { 
xhr.setRequestHeader("Authorization", "Basic" + btoa('b110030d-7491-48fe-9354-05c0ec0181d7' + ":" + '')); 
}, 
data: { username: 'b110030d-7491-48fe-9354-05c0ec0181d7', password:'' }, 
success: function (data) { 
console.log(data.results);  

     if(data.results.length > 0) { 
      var numberOfResults = data.results.length; 
      $('.reed').append('<div>Found '+numberOfResults+' results</div>'); 
      $.each(data.results, function(index, element) { 
       if(index < 100) { 
        $('.reed').append('<div class="my-class">'+element.locationName+'<div><div class="my-class2">'+element.jobDescription+'</div>'); 
       } 
      }); 
     } else { 
      $('.reed').append('<div class="my-class">Sorry nothing found</div>'); 
     } 


    } 
}); 

}); 
+0

JSONP將無法正常工作。您不能使用它設置自定義請求標頭(即使您可以盲目推薦JSONP而不檢查服務器支持,它也不是非常有用)。服務器必須授權CORS。 – Quentin

+0

它以什麼方式不起作用?控制檯是否顯示任何錯誤?網絡標籤是否顯示正在發出的請求?它是否顯示回覆?它們的格式是否與您期望的數據一樣? – Quentin

+0

Chrome出現了這個控制檯錯誤:加載資源失敗:服務器響應狀態爲405(方法不允許) index.html:1 XMLHttpRequest無法加載http://www.reed.co.uk/api/ 1.0 /搜索?關鍵字= imobilus%20jobs&用戶名= b110030d-7491-48fe-9354-05c0ec0181d7&密碼=。無效的HTTP狀態碼405 –

回答

1

我只是檢查使用dataType: 'jsonp'您的代碼,並在Chrome正常工作爲好。

我測試它在鉻和它的作品。然而,有認證的Firefox的問題,但我認爲是不相關的這個

$(document) .ready(function() { 

$.ajax({ 
    type: 'GET', 
    url: 'http://www.reed.co.uk/api/1.0/search?keywords=imobilus%20jobs', 
    beforeSend: function(xhr) { 
xhr.setRequestHeader("Authorization", "Basic " + btoa(' b110030d-7491-48fe-9354-05c0ec0181d7' + ":" + '')); 
}, 
    data: { username: 'b110030d-7491-48fe-9354-05c0ec0181d7', password:'' }, 
    dataType: 'jsonp', 
    success: function (data) { 
     console.log(data.results); 

     if(data.results.length > 0) { 
      var numberOfResults = data.results.length; 
      $('.container').append('<div>Found '+numberOfResults+' results</div>'); 
      $.each(data.results, function(index, element) { 
       if(index < 100) { 
        $('.container').append('<div class="my-class">'+element.locationName+'<div><div>'+element.jobDescription+'</div>'); 
       } 
      }); 
     } else { 
      $('.container').append('<div class="my-class">Sorry nothing found</div>'); 
     } 


    } 
}); 

}); 
相關問題