【问题标题】:Return json object to client callback function将json对象返回给客户端回调函数
【发布时间】:2014-03-17 01:00:21
【问题描述】:

我在我的 C# aspx 页面中注册了一个客户端回调函数。我想从 receiveServerResponse 返回一个 javascript 对象,但我得到了一个字符串。如何让 receiveServerResponse 的输入成为 json 对象?我不想在这里使用 eval。

//in aspx.cs page:
String cbReference =
            Page.ClientScript.GetCallbackEventReference(this,
            "arg", "receiveServerResponse", "context");
String callbackScript;
callbackScript = "function CallServer(arg, context)" +
            "{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
            "CallServer", callbackScript, true);

//further down in aspx.cs page
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public String GetCallbackResult()

//in aspx page:
var receiveServerResponse = function (evalText) {

【问题讨论】:

    标签: c# javascript asp.net json callback


    【解决方案1】:

    您的问题是在 JavaScript 方面还是在 C# 方面?听起来值“evalText”是作为字符串传递的,如果是这种情况,那么这可能会有所帮助:

    var jsonObject = JSON.parse(evalText);
    

    如果您将值从 JS 返回到 C#,那么相反的方法应该可以解决问题:

    return JSON.stringify(jsonObject);
    

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2021-04-09
      相关资源
      最近更新 更多