在看了這裏的幾個問題/答案後,我沒有看到我認爲我需要的東西。我有一個頁面通過ajax發佈到一個asp服務器頁面。返回的json字符串顯示在Firefox的控制檯上。我能看到該信息的數據去到ASP頁,我可以看到這是從經典的asp服務器解析JSON數據?
{「名字」的迴應:「克里斯托弗」,「姓氏」:「羅梅羅」,「電子郵件」:「[email protected] 「,」adminlvl「:」00「,」message「:」感謝登錄!「 }
我還可以在控制檯 - >所有 - > JSON的JSON選項卡中看到上面字符串的值。控制檯內沒有報告錯誤。下面是我的javascript:
$('#loginsub').click(function() {
$.ajax({
url: "logincheck.asp",
type: "POST",
data: $('#loginform').serialize(),
dataType: "json",
success: function(data) {
console.log(data);
//alert(data.firstname + ' ' + data.lastname);
//alert(data[0].firstname + ' ' + data[0].lastname);
$.trim(data);
var json = $.parseJSON(data);
alert(json.firstname);
}
});
});
這裏是在服務器上運行的logincheck.asp的ASP:在JavaScript沒有返回,我會期待值
set cmd = Server.CreateObject("ADODB.Command")
with cmd
.ActiveConnection = cnnopen
.CommandText = storedproc
.CommandType = adCmdStoredProc
dim intCount,intItem
for each item in odcformdata
select case vartype(odcformdata(item)) 'this is searching for the correct data type to put into the parameter [type] argument below. (integers, currency, dates, & strings)
case 2 : .Parameters.Append .CreateParameter("@"&cstr(item),adInteger,adParamInput,len(odcformdata(item)),odcformdata(item))
case 6 : .Parameters.Append .CreateParameter("@"&cstr(item),adCurrency,adParamInput,len(odcformdata(item)),odcformdata(item))
case 7 : .Parameters.Append .CreateParameter("@"&cstr(item),adDate,adParamInput,len(odcformdata(item)),odcformdata(item))
case 8 : .Parameters.Append .CreateParameter("@"&cstr(item),adVarChar,adParamInput,len(odcformdata(item)),odcformdata(item))
end select
next
end with
set rs = cmd.execute
'do stuff with returned results from select or leave blank if insert/delete/etc stored procedure
if rs.EOF = false then
'Build json array based on fields returned from stored proc.
dim arrJSON
arrJSON = "{ "
while not rs.EOF
for each fields in rs.Fields
arrJSON = arrJSON & """" & fields.name & """: """ & fields & ""","
next
rs.movenext
wend
arrJSON = arrJSON & """message"": ""Thanks for logging in!"","
arrJSON = left(arrJSON, len(arrJSON)-1) & " }"
response.write arrJSON
end if
set rs = nothing
set cmd = nothing
odcformdata.removeall
警報()當我期望JSON數組/字符串被打印出來時,我得到[object Object]返回警報。
任何人對我有一些建議?我是一名jquery菜鳥,並且變得越來越好,但是這讓我很緊張!
jQuery將解析JSON你。也就是說,數據參數應該已經是一個對象,第一個(註釋)警報應該可以工作。你不需要調用$ .parseJSON()。 – nnnnnn 2012-03-22 04:13:20
如果您使用console.log(json)而不是alert(json.firstname),那麼輸出是什麼? – Tuan 2012-03-22 04:14:23