여기 몇 가지 질문/답변을 살펴본 후에 필자는 내가 필요한 것으로 생각하지 않는다. 나는 ajax를 통해 ASP 서버 페이지에 게시하는 페이지를 가지고있다. 돌아 오는 json 문자열이 Firefox의 콘솔에 표시됩니다. 포스트 데이터가 ASP 페이지로 이동하는 것을 볼 수 있습니다. 응답은고전적인 ASP 서버에서 JSON 데이터를 파싱 하시겠습니까?
{ "firstname": "Christopher", "lastname": "Romero", "email": "[email protected] ","adminlvl ":"00 ","message ":"로그인 해 주셔서 감사합니다! " }
위의 문자열 값은 Console -> ALL -> JSON의 JSON 탭에서도 확인할 수 있습니다. 콘솔에보고되는 오류가 없습니다. 여기
$('#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
경고를() 자바 스크립트에서 내가 기대 값을 반환하지 않습니다, JSON 배열/문자열이 출력 될 것으로 예상되면 [object Object]가 경고에 반환됩니다.
나를위한 조언이있는 사람은 누구입니까? 나는 jquery 신참이며 더 좋아지고 있지만 이것은 내 신경을 얻고 있습니다!
jQuery를 당신을 위해 JSON을 구문 분석합니다. 즉, 데이터 매개 변수가 이미 객체이어야하고 첫 번째 (주석 처리 된) 경고가 작동해야합니다. $ .parseJSON()을 호출 할 필요는 없습니다. – nnnnnn
alert (json.firstname) 대신 console.log (json)를 수행하면 어떤 결과가 나옵니까? – Tuan