Domanda

Può essere, ma non è così problematico per voi. Ma sto cercando prima volta con serializzazione JSON. e leggere anche altri articoli in stackowerflow.

Ho creato Entity Framework modello di dati. poi con il metodo di ottenere tutti i dati da un oggetto:

private uqsEntities _db = new uqsEntities();
//get all data from table sysMainTableColumns where tableName=paramtableName
public List<sysMainTableColumns> getDataAboutMainTable(string tableName)
{
     return (from column in _db.sysMainTableColumns
                    where column.TableName==tableName
                    select column).ToList();

}

il mio webservice:

public string getDataAboutMainTable()
{
    penta.DAC.Tables dictTable = new penta.DAC.Tables();
    var result = dictTable.getDataAboutMainTable("1");
    return new JavaScriptSerializer().Serialize(result);
}

e jQuery metodo ajax

$('#loadData').click(function() {
            $.ajax({
                type: "POST",
                url: "WS/ConstructorWS.asmx/getDataAboutMainTable",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    $("#jsonResponse").html(msg);

                    var data = eval("(" + msg + ")");
                    //do something with data
                },
                error: function(msg) {

                }
            });
        });

fallisce (da fairbug):

missing ] after element list [Break on this error] var data = eval("(" + msg + ")");

Risposta ajax (con Firebug se tolgo var data = eval("(" + msg + ")")):

{"d":"[{\"ID\":1,\"TableName\":\"1\",\"Name\":\"d\",\"FullName\":\"f\",\"Type\":\"nvarchar(50)\",\"MeasurementUnit\":\"t         \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":1}],\"IsTemporary\":false}},{\"ID\":2,\"TableName\":\"1\",\"Name\":\"e\",\"FullName\":\"e\",\"Type\":\"int\",\"MeasurementUnit\":\"r         \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":2}],\"IsTemporary\":false}}]"}

problema con la dati , il codice non riesce lì. e penso che io non sono uso JavaScriptSerializer (). Serialize () metodo molto bene.

Per favore, dimmi, che un errore grande che ho fatto in codice C #?

È stato utile?

Soluzione

  1. Non è necessario eval. jQuery che fa per voi quando si specifica dataType: "json"
  2. E 'una buona idea per serializzare entità direttamente come JavaScriptSerializer morirà se ci capita di contenere un riferimento circolare.
  3. Non dimenticare il d! Questo è inserito dai servizi WCF per aggirare un buco di sicurezza in alcuni browser quando l'oggetto radice è un array.

Altri suggerimenti

Hai provato il debug con Firebug o Fiddler per vedere che cosa il contenuto JSON assomiglia?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top