2012-03-22 147 views
0

在看了这里的几个问题/答案后,我没有看到我认为我需要的东西。我有一个页面通过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菜鸟,并且变得越来越好,但是这让我很紧张!

+0

jQuery将解析JSON你。也就是说,数据参数应该已经是一个对象,第一个(注释)警报应该可以工作。你不需要调用$ .parseJSON()。 – nnnnnn 2012-03-22 04:13:20

+0

如果您使用console.log(json)而不是alert(json.firstname),那么输出是什么? – Tuan 2012-03-22 04:14:23

回答

0

你的第一个console.log(data)显示什么?

这个工作对我来说: http://jsfiddle.net/2tvCf/

+0

console.log显示我:{“firstname”:“Christopher”,“lastname”:“Romero”,“email”:“[email protected]”,“adminlvl”:“00”,“message”:“感谢您登录!“ } – digitalcb 2012-03-22 05:07:26

+0

我认为有人看着它的事实使它工作......有点像在量子物理学中观察时电子同时在两个地方。大声笑我没有注意到第一次测试警报,我把它放在那里,它表现得很好。在警报框中显示[对象对象]之前。感谢您的帮助...我真的很欣赏这个外观! – digitalcb 2012-03-22 05:19:13