2015-11-20 47 views
0

我想這個jQuery代碼:JQuery的歸國從PHP JSON字符串未定義

val = $(this).val(); 
var data = { 
    "action": "test" 
}; 
data = $(this).serialize() + "&" + $.param(data); 
$.ajax({ 
    type: "POST", 
    dataType: "json", 
    url: "?getCustomer=1&sequence="+val+"", 
    data: data, 
    success: function(data) { 
     alert(data["sequence"]); 
    } 
}); 

alert正在返回undefined

如果我查詢的網址(?getCustomer=1&sequence=4)我得到這個返回:

[{"sequence":"53"}] 

所以序列值肯定是

這就是示出了在控制檯:

[Object]0: Objectsequence: "112"__proto__: Objectlength: 1__proto__: Array[0]concat: concat()constructor: Array()copyWithin: copyWithin()entries: entries()every: every()fill: fill()filter: filter()find: find()findIndex: findIndex()forEach: forEach()indexOf: indexOf()join: join()keys: keys()lastIndexOf: lastIndexOf()length: 0map: map()pop: pop()push: push()reduce: reduce()reduceRight: reduceRight()reverse: reverse()shift: shift()slice: slice()some: some()sort: sort()splice: splice()toLocaleString: toLocaleString()toString: toString()unshift: unshift()Symbol(Symbol.iterator): values()Symbol(Symbol.unscopables): Object__proto__: Object 
+0

是什麼的console.log(數據)顯示? – ahervin

+0

它已經有 – charlie

+0

檢查我的更新 - wouldnt適合評論 – charlie

回答

2

你誤解了你的json。變化

alert(data["sequence"]); 

隨着

alert(data[0]["sequence"]); 

或更好

alert(data[0].sequence); 
0
[{"sequence":"53"}]

是一個數組中,第一元件,其是具有序列構件的對象。您需要alert(data[0]['sequence'])