2012-09-06 49 views
0

可能重複:
How to use split?分手AJAX返回

我試圖創建一個基於掀起了插件的動態內容股利。默認情況下,插件會獲取幻燈片對象並在逗號處分解它。每個逗號後面的每個字符串都是分開的。但是,當通過AJAX返回值時,它將被解釋爲一個長字符串,而不是被分解。我怎樣才能讓jQuery將它分成多個字符串?

Ajax的:被返回

$.ajax({ 
url: 'backend/index.php', 
data: {'check':'true'}, 
type: 'POST', 
async: false, 
success: function(data) { 
    var carousel, 
    el, 
    i, 
    page, 
    slides = [ data ]; 
} 
}; 

當前數據:

echo " 
    '<strong>Swipe</strong> to know more &gt;&gt;&gt;<br>Or scroll down for <em>Lorem Ipsum</em>', 
    '1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.', 
    '2. A robot must obey the orders given to it by human beings, except where such orders would conflict with the First Law.', 
    '3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws.' 
"; 
exit(); 

幻燈片的默認例如:

slides = [ 
    '<strong>Swipe</strong> to know more &gt;&gt;&gt;<br>Or scroll down for <em>Lorem Ipsum</em>', 
    '1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.', 
    '2. A robot must obey the orders given to it by human beings, except where such orders would conflict with the First Law.', 
    '3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws.' 
]; 

作爲參考,這是使用SwipeView插件發現這裏:http://cubiq.org/swipeview

+0

爲您的字符串添加一個分隔符並將其分隔在分隔符(例如|和data.split(「|」))上,或者將數據作爲有效的JSON返回並將數據類型更改爲JSON –

+2

您的原因不返回一個JSON對象開始是? – epascarello

+1

'async:false' - 爲什麼,哦爲什麼??? :) – Andreas

回答

1

哦,這是一個糟糕的問題。提交後幾分鐘,我意識到它是多麼容易。

編輯後端PHP返回:

$result = array('<strong>Swipe</strong> to know more &gt;&gt;&gt;<br>Or scroll down for <em>Lorem Ipsum</em>','1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.','2. A robot must obey the orders given to it by human beings, except where such orders would conflict with the First Law.','3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws.'); 

echo json_encode($result); 

然後簡單地添加到AJAX:

dataType: "json", 

感謝您的幫助大家!