2009-07-22 5 views
1

Ich habe folgende Ajax-Aufruf:

var empId = $(this).attr('name').replace(/disp/,''); 
    $.ajax({ 
     url: '<%= Url.Action("AjaxDisp") %>', 
     data: { id: empId, format: 'json' }, 
     dataType: 'json', 
     success: function(data,status) { 
     // populate the popup and display it    
     } 
    }); 

EmpID ist es (ich benutze alert (EmpID), es zu testen, ist es es in Ordnung). Aber in Action-Methode AjaxDisp (Int ID, Strng Format), kann ich die ID bekommen, aber ich kann Format = "JSON" bekommen.

Warum?

+0

Können Sie dem Post-Header einen Inhalt anzeigen, wie er in Firebug angezeigt wird? –

Antwort

1

Konvertieren Sie das Objekt (übergeben an Daten) in JSON-Zeichenfolge.

var empId = $(this).attr('name').replace(/disp/,''); 
    $.ajax({ 
     url: '<%= Url.Action("AjaxDisp") %>', 
     data: JSON.stringify({ id: empId, format: 'json' }), //converting your object to JSON string. 
     dataType: 'json', 
     success: function(data,status) { 
     // populate the popup and display it    
     } 
    }); 
+0

Danke. Wenn Sie es versuchen und es ausführen. Es heißt, dass JSON nicht definiert ist. – KentZhou

+0

Sie müssen diese JSON-Bibliothek einschließen http://www.json.org/json2.js – SolutionYogi

+1

Der native JSON-Parser ist in Internet Explorer 8 und FireFox 3.1 verfügbar. Für andere Browser benötigen Sie die JSON-Bibliothek. –