2012-03-22 3 views
0

여기 몇 가지 질문/답변을 살펴본 후에 필자는 내가 필요한 것으로 생각하지 않는다. 나는 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 신참이며 더 좋아지고 있지만 이것은 내 신경을 얻고 있습니다!

+0

jQuery를 당신을 위해 JSON을 구문 분석합니다. 즉, 데이터 매개 변수가 이미 객체이어야하고 첫 번째 (주석 처리 된) 경고가 작동해야합니다. $ .parseJSON()을 호출 할 필요는 없습니다. – nnnnnn

+0

alert (json.firstname) 대신 console.log (json)를 수행하면 어떤 결과가 나옵니까? – Tuan

답변

0

첫 번째로 표시되는 console.log(data)은 무엇인가요?

이 나를 위해 작동합니다 가 http://jsfiddle.net/2tvCf/

+0

console.log에 다음과 같은 메시지가 표시됩니다. { "firstname": "Christopher", "lastname": "Romero", "email": "[email protected]", "adminlvl": "00", "message": " 로그인 해 주셔서 감사합니다! " } – digitalcb

+0

저는 누군가가 그것을 보았다는 사실이 그것을 작동하게 만들었다 고 생각합니다. 양자 물리학에서 관찰 될 때 한 번에 두 곳의 전자가있는 것처럼 보입니다. 나는 거기에 넣은 첫 번째 테스트 경고문의 주석을 달았으며 괜찮았다. 경고 상자에 [object Object]가 표시되기 전에 도와 줘서 고마워 ... 정말 고마워! – digitalcb

관련 문제