2013-03-08 13 views
17

Ho un codice come questo: sto usando il valore Data come oggetto letterale, invece di concatenare una stringa. PERCHÉ? see hereMessaggio: primitiva JSON non valida: metodo jquery ajax con C# Webmethod

il mio codice è questo: -

$.ajax({ 
        url: "../Member/Home.aspx/SaveClient", 
        type: "POST", 
        async: false, 
        dataType: 'json', 
        contentType: 'application/json; charset=utf-8', 
        data: { 
         "projectSoid": ProjectId, 
         "startDate": StartDate, 
         "endDate": EndDate, 
         "clientManager": ClientManager 
        }, 
        success: function (response) { 
         if (response.d != "") { 

         } 
        }, 
        error: function (response) { 
         var r = jQuery.parseJSON(response.responseText); 
         alert("Message: " + r.Message); 
         alert("StackTrace: " + r.StackTrace); 
         alert("ExceptionType: " + r.ExceptionType); 
        } 
       }) 

e webmethod è come questo:

[WebMethod] 
     public static string SaveClient(string projectSoid, string startDate, string endDate, string clientManager) 
     {} 

Il problema è che ho ottenuto errore come questo:

messaggio: Invalid JSON primitiva : projectSoid

+6

È necessario JSON.strigify i dati: 'data: JSON.strigify ({ " projectSoid ": ProjectId, " startDate ": StartDate, " endDate ": EndDate, " clientManager ": Clien tManager }), ' – nemesv

+0

Qualche commento sul mio commento? L'hai provato? Ha funzionato? – nemesv

+0

non so cosa sia JSON.strigify? Ho un errore che non funziona :( –

risposta

36

Con il tuo contentType: 'application/json; charset=utf-8' stai sostenendo che invierai JSON ma attualmente la tua proprietà data non supporta JSON.

È necessario trasformare il vostro data a JSON con il metodo JSON.stringify:

Quindi cambiare la vostra proprietà data a:

data: JSON.stringify({ 
    "projectSoid": ProjectId, 
    "startDate": StartDate, 
    "endDate": EndDate, 
    "clientManager": ClientManager 
}), 

Si dovrebbe notare che il metodo JSON.stringify non è nativamente supportato nei browser meno recenti in modo potrebbe essere necessario fornire un'implementazione utilizzando una delle varie librerie come:

Douglas Crockford's JSON2 library.

+0

ok, grazie proverò :) –

+0

@nemesv: Grazie Nemev: Solo una correzione nel tuo codice ... U ortografia errata stringify come strigify ... – Saravanan

+0

@NestorC: puoi accettare questo come risposta se soddisfa le tue esigenze? In modo che possa aiutare qualcuno ... – Saravanan

1

JavaScript sul lato client

var items = [{ projectSoid: ProjectId, startDate: StartDate, endDate: EndDate, clientManager: ClientManager }]; 


        $.ajax({ 
         url: '"../Member/Home.aspx/SaveClient', 
         type: "POST", 
         data: JSON.stringify({ items: items }), 

         //data: JSON.stringify("{DocKey : '" + DocKey + "',highlightText: '" + JSON.stringify(text) + "',pageNo: '" + pgNo + "',left: '" + left + "',top: '" + top + "',width: '" + width + "',height: '" + height + "'}"), 

         //data: "{DocKey\":\""+ DocKey+"\",\"highlightText\":\""+ text +"\",\"pageNo\":\""+pgNo+"\",\"left\":\""+left+"\",\"top\":\""+top+",\"width\":\""+width+"\",\"height\":\""+ height +"}}", 
         // data: "{DocKey : '" + DocKey + "',highlightText: '" + text + "',pageNo: '" + pgNo + "',left: '" + left + "',top: '" + top + "',width: '" + width + "',height: '" + height + "'}", 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         beforeSend: function() { 
          alert("Start!!! "); 
         }, 
         success: function (data) { 
          alert("Save data Successfully"); 
         }, 
         failure: function (msg) { alert("Sorry!!! "); evt.obj.deleteObject(); }, 
         async: false 

        }); 

Web Metodo in codice dietro

[WebMethod]  
public static string SaveClient(object items)  { 

    List<object> lstItems = new  JavaScriptSerializer().ConvertToType<List<object>>(items); 

    Dictionary<string, object> dic = (Dictionary<string, object>)lstItems[0]; 

    } 
Problemi correlati