2014-01-07 81 views
0

令牌時我只是試圖使用的處理程序,我產生一個令牌,並返回到客戶端進行身份驗證。 (目的:驗證與令牌的所有請求,比第一認證步驟等)ParseError使用AJAX或ASHX處理程序

我測試客戶端確實獲得令牌,但與「parsererror」狀態和「意外的標記T」所描述的錯誤在嘗試涉及通過令牌進行身份驗證的方法時會給出語法錯誤

代碼來生成令牌這是運作良好

 byte[] time = BitConverter.GetBytes(DateTime.Now.ToBinary()); 
     byte[] key = Guid.NewGuid().ToByteArray(); 
     string _token = Convert.ToBase64String(time.Concat(key).ToArray()); 

Ajax代碼爲測試請求發送請求

function test() { 
     var jsonParam = { token: _token , type: "check" } 
     $.ajax({ 
      url: "Test.ashx", 
      type: "post", 
      data: JSON.stringify(jsonParam), 
      dataType: "json", 
      contentType: 'application/json; charset=utf-8', 
      async: false, 
      success: function (response) { 

       document.getElementById('eCode').innerHTML = response.eCode; 

      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
       alert(XMLHttpRequest.responeText + "\r\nStatus: " + textStatus + "\r\n" + "Error: " + errorThrown); 
      } 
     }); 

    } 

服務器代碼,這個AJAX請求作出迴應

if (postType == "check") 
    { 
     // (dict is the deserialized received JSON, a dictionary of [string, object] 

     string _token = dict["token"] as string; 
     byte[] data = Convert.FromBase64String(_token); 
     DateTime when = DateTime.FromBinary(BitConverter.ToInt64(data, 0)); 
     if (when < DateTime.Now.AddHours(-12)) 
     { 
      // Too old 
      context.Response.Write(new Code { eCode = "old" }); 
     } 
     else if (when > DateTime.Now.AddHours(1)) 
     { 
      // Impossible 
      context.Response.Write(new Code { eCode = "impossible" }); 
     } 
     else 
     { 
      // Good 
      context.Response.Write(new Code { eCode = "Time: " + when.ToString("dd/MM/yyyy") }); 
     } 


    } 

並收到以下錯誤:parsererror,「意外標記T」的語法錯誤。此錯誤出現在客戶端警報 找不到任何問題完全相同的人。

+0

你從哪裏得到這個錯誤嗎?客戶端或服務器?什麼行代碼? –

+0

錯誤來自於客戶端 – Kfirprods

+0

的警覺,這是什麼給你? –

回答

1

由通用處理器返回的JSON格式不正確或者甚至沒有JSON。您可以通過將dataType屬性設置爲text來檢查處理程序的輸出嗎?就像這樣:dataType: "text"

+0

你說得對,我剛纔忘了序列化「守則」的對象,多麼愚蠢的錯誤!謝謝。 – Kfirprods