2012-09-16 113 views
2

更新 現在我除了調整工作代碼之外沒有收到任何東西。 這個邏輯有什麼問題? 我使用dataType創建$ .ajax調用:'jsonp',這是跨域調用的唯一方式。 我知道,這個調用需要'jsonp'類型,但是接收'text/html'。我如何解析這個響應(轉換,預處理,過濾器)?

謝謝!

這是一個問題。 我只是想得到使用jSON的想法。 這是一個工程。

$(document).ready(function(){ 
    $('ul li a').click(function(){ 
    var test = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="www.mohtasebi.com/blog/2011/06/cross-domain-ajax-using-jquery-the-ultimate-guide/"') + '&format=xml&callback=?'; 
    //var test = "http://mohtasebi.com/blog/2011/06/cross-domain-ajax-using-jquery-the-ultimate-guide/"; 
    loadPage(test); 
    }); 
}); 

function loadPage(test) 
{ 
    $.ajax({ 
    url:test, 
    dataType:'jsonp', 
    crossDomain: 'true', 
    success: function(data){  
    if (data.results[0]) {  
    alert("ok") 
    } } 
}); 

} 

但是,如果使用評論URL

var test = "http://mohtasebi.com/blog/2011/06/cross-domain-ajax-using-jquery-the-ultimate-guide/";

,而不是YUI converted我得到這個錯誤:

Resource interpreted as Script but transferred with MIME type text/html: "http://mohtasebi.com/blog/2011/06/cross-domain-ajax-using-jquery-the-ultimate-guide/?callback=jQuery18104882542605046183_1347794498881&_=1347794500464". jquery.min.js:8169 Uncaught SyntaxError: Unexpected token < mohtasebi.com:1

我在做什麼錯。謝謝!

回答

0

使用$.getJSON,這簡單得多。

var test = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="www.mohtasebi.com/blog/2011/06/cross-domain-ajax-using-jquery-the-ultimate-guide/"') + '&format=xml&callback=?'; 
$.getJSON(test, function (data) { 
    console.log(data); 
}); 

The demo.

+0

謝謝你,但我的問題是與評論的網址,使用YUI不coverted。 – Dmitry

+0

http://jsfiddle.net/reeFj/ – Dmitry

+0

@Dmitry該網址不會返回jsonp響應,你想要什麼? – xdazz

2
var test = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="www.mohtasebi.com/blog/2011/06/cross-domain-ajax-using-jquery-the-ultimate-guide/"') + '&format=xml'; 

$.ajax({ 
    url: test, 
    dataType: 'jsonp', 
    jsonpCallback: 'blah', // just add this and remove &callback=? from url last 
    crossDomain: 'true', 
    success: function(data) { 
     console.log(data); // see the console for data 
     if (data.results[0]) { 
      alert('OK'); 
     } 
    } 
});​ 

添加jsonpCallback阿賈克斯配置和刪除&回調=?從url的最後部分。

Demo

+0

這就是我嘗試開始工作http://jsfiddle.net/reeFj/ – Dmitry