2013-07-05 18 views
0

我收到以下錯誤,當嘗試從asp.net函數讀取JSON數據,這裏是圖像誤差 enter image description here獲取的翻譯:錯誤呼籲JSON數據的JQuery的Ajax功能時

這裏是jQuery代碼,

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#getdata").click(function() { 
      $.ajax({ 
       type: "POST", 
       url: "Default.aspx/GetData",     
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { 
        alert(data.d); 
        // $("#company").html(data.d); 
         console.log(data); 
       }, 
       error: function (error) { 
        alert(error); 
       } 
      }); 
     }); 
    }); 
</script> 

我做了什麼錯誤來得到這個錯誤。感謝您的幫助。 從控制檯日誌收到此錯誤,console.log(error) enter image description here

+0

你從asp.net方法返回什麼? – Adil

+0

它的結果不是錯誤,您的console.log成功說明了什麼? –

+0

這是因爲alert()不是調試工具,所以應該使用控制檯。 – adeneo

回答

1

的原因,你所看到的那就是data.d是代表返回的JSON文本響應的對象。當您將對象傳入alert時,它會顯示[object Object]。無論您正在尋找什麼,都將是data.d的財產。我會在該行上放置一個斷點並查看可用的屬性。它會像data.d.myProperty那樣,它將包含您正在嘗試使用的實際字符串/ HTML。

0

不要使用警報!這隻會顯示這一點。 由於你的響應是一個Json對象,請嘗試JSON.stringify它或使用console.log來查看數據

+0

我在成功和錯誤部分添加了「console.log(JSON.stringify(data.d))」,但沒有從Chrome控制檯獲取任何東西 –

+0

只是做了這樣的console.log(數據): success:function數據){ console.log(data); } – neoeahit

+0

它沒有顯示任何內容,我需要在Chrome中進行任何設置嗎? –

0

最後,我發現有什麼問題,我必須添加「[WebMethod()]」聲明就在asp.net函數之前。例如,

[WebMethod()] 
    public static string GetData() 
    { 

     try 
     { 
      string strjson; 
      string connectionstring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; 
      SqlConnection Con = new SqlConnection(connectionstring); 
      DataSet DS = new DataSet(); 
      String CmdText = "Select Compname,compadd1,compadd2,compemail from aas_company where [email protected]"; 
      SqlCommand cmd = new SqlCommand(CmdText, Con); 
      cmd.Parameters.Add("@cmpid", SqlDbType.Int).Value = 22; 
      Con.Open(); 
      SqlDataAdapter DA = new SqlDataAdapter(cmd); 
      DA.Fill(DS); 
      DataTable dTable = DS.Tables[0]; 
      strjson = GetJSONString(dTable); 
      Con.Close();    
      return strjson; 
     } 
     catch (Exception ex) 
     { 
      throw new System.Exception("Error In Get Data" + ex.Message); 
     } 
    } 

這個答案不適合專家只爲初學者,謝謝。