ここでいくつかの質問/答えを見た後、私は私が必要と思うものは見ていない。私はajax経由でaspサーバーページにポストするページを持っています。戻るjson文字列がFirefoxのコンソールに表示されています。投稿データがASPページに表示されるのを見ることができます。従来のASPサーバーからJSONデータを解析するには?
{"firstname": "Christopher"、 "lastname": "Romero"、 "email": "[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です:
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
JavaScriptでアラートが()私は期待値を返すされていません、 JSON配列/文字列が出力されることを期待しているときに、[オブジェクトオブジェクト]がアラートに戻ってきています。
誰かの助言がありますか?私はjquery新人だし、良くなっていますが、これは私の神経に乗っています!
jQueryのはあなたのためにJSONをパースします。つまり、dataパラメータはすでにオブジェクトであり、最初の(コメント付きの)アラートが機能するはずです。 $ .parseJSON()を呼び出す必要はありません。 – nnnnnn
alert(json.firstname)の代わりにconsole.log(json)を実行した場合の出力は何ですか? – Tuan