2013-11-15 35 views
2

有人可以解釋我爲什麼這給了我一個錯誤?Ajax請求與網絡方法

我的ajax調用了這樣的東西。

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html> 
<html> 

<head> 
    <meta charset="utf-8"> 

    <title></title> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

    <script> 

     $(document).ready(function() { 

      $('#btn1').click(function() { 
       var values = JSON.stringify({ data: $('#form1').serializeArray() }); 
       alert($('#form1').serializeArray()); 
       $.ajax({ 
        type: "POST", 
        url: "Default.aspx/Test", 
        contentType: "application/json; charset=utf-8", 
        scripts: true, 
        dataType: "json", 
        data: values, 
        success: function (data) { $('#results').append(data.d); }, 

        error: function() { $('#results').append('hata'); } 
       }); 
      });  }); 

      </script> 





</head> 
<body> 


    <form runat="server" id="form1"> 
First name: <input type="text" name="firstname"><br> 
Last name: <input type="text" name="lastname"> 

     <button id="btn1" type="button">bummm</button> 
        <div id="results"></div> 

     </form> 
</body> 
</html> 





[WebMethod] 

    public static string Test (string data) 
     { 

     return "İşlem başarılı"+data; 
     } 

它說我{「消息」:「不支持數組的反序列化類型\ u0027System.String \ u0027」,「堆棧跟蹤」:」

+0

什麼是錯誤訊息? –

+0

錯誤:function(){alert('error'); }這個回報我 – sakir

+0

我知道,但是當你運行代碼時你收到的信息是什麼? 嘗試改變你的錯誤方法'錯誤:功能(錯誤){警報('錯誤'); }'並調試並察看'err'變種。 –

回答

2

我想這是因爲你錯了打電話給你的WebMethod阿賈克斯 你的WebMethod有一個參數命名datastring類型,但你嘗試發送沒有名字,所以嘗試改變你這樣的代碼:

var KaydetDataWithAjax = function (e) 
{ 
     var values =JSON.stringify({data: $(e).serializeArray()}); 

     alert(values); 
     $.ajax({ 
      type: "POST", 
      dataType: 'json', 
      contentType: "application/json; charset=utf-8", 
      url: "Harita.aspx/HaritaKaydet", 
      scripts: true, 
      data:values, 
      success: function (dt) { alert(dt);}, 
      complete:function(){}, 
      error: function() { alert('error'); } 
     }); 
    }; 

UPDATE

上新項目這種方法工作

$.ajax({ 
     type: "POST", 
     dataType: 'json', 
     contentType: "application/json; charset=utf-8", 
     url: "Harita.aspx/HaritaKaydet", 
     scripts: true, 
     data:JSON.stringify({data: 'text'}), 
     success: function (dt) { alert(dt);}, 
     complete:function(){}, 
     error: function() { alert('error'); } 
    }); 

,如果在你的情況下,它不工作,那麼可能的幫助,如果你提供多一點的代碼

更新2
原來它比一切從簡我想!
serializeArray()返回Array!因此,找到與參數類似List<object>服務器的方法,所以要解決必須字符串化陣列太
所以試試這個代碼

var KaydetDataWithAjax = function (e) 
{ 
    var values =JSON.stringify({data: JSON.stringify($(e).serializeArray())}); 

    alert(values); 
    $.ajax({ 
     type: "POST", 
     dataType: 'json', 
     contentType: "application/json; charset=utf-8", 
     url: "Harita.aspx/HaritaKaydet", 
     scripts: true, 
     data:values, 
     success: function (dt) { alert(dt);}, 
     complete:function(){}, 
     error: function() { alert('error'); } 
    }); 
}; 
+0

仍然出錯。 – sakir

+1

嘗試'數據:JSON.stringify({數據:'測試'})' – Grundy

+0

仍然同樣的問題繼續我得到錯誤 – sakir