2011-08-12 77 views
1

我有一個字符串,因爲'& q'(我猜)被原始字符串轉義而過早終止。如果我想在PHP中保留原始字符串,應該如何處理這個問題?JSON轉義字符

原始字符串

'http://answers.onstartups.com/search?tab=active&q=fbi' 

的var_dump結果

'["http://answers.onstartups.com/search?tab=active' 

JS

var linksStr = $("#links").val(); 
var matches = JSON.stringify(linksStr.match(/\bhttps?:\/\/[^\s]+/gi)); 

    $.ajax({ 
     type: 'GET', 
     dataType: 'json', 
     cache: false, 
     data: 'matches=' + matches, 
     url: 'publishlinks/check_links', 
     success:      
     function(response) { 
     alert(response); 

     } 
    })  

check_links

$urls = $this->input->get('matches');   
var_dump($urls); 
+0

轉發不讚賞。 http://stackoverflow.com/questions/7044409/json-escaped-character – mario

回答

2

變化data: 'matches=' + matches,

要:data: {"matches": matches},

因此,jQuery會找出你的編碼。否則,你將不得不使用編碼encodeURIComponent()

3

可以編碼JSON字符串:

data: 'matches=' + encodeURIComponent(matches), 

你也可以寫這樣的:

data: { matches: matches } 

,然後jQuery的應該爲你做編碼的一步。

3

URL中的URI從jQuery的.VAL(返回)是:

'http://answers.onstartups.com/search?tab=active&q=fbi' 

.match()正則表達式會返回一個數組:

new Array("http://answers.onstartups.com/search?tab=active&q=fbi") 

哪JSON.stringify()正確輸出爲:

["http://answers.onstartups.com/search?tab=active&q=fbi"] 

但是,我F你重視它作爲原料 GET參數在這裏:

data: 'matches=' + matches, 

然後在URL將終止GET值enescaped &。使用encodeURIComponent

+0

謝謝。抱歉關於轉發:) –