对象变量转换成JSON字符串asp.net页面的方法 [英] converting object variable to json string for asp.net page method

查看:119
本文介绍了对象变量转换成JSON字符串asp.net页面的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常简单的任务来执行,但我承担的风险要问无妨。

This is probably a very simple task to perform but I'm taking the risk to ask anyway.

我有一个对象变量,看起来像这样:

I have an object variable that looks like this:

var MyObj = {"Param1": "Default",
             "Param2": "test",
             "Param3": 3 };

我使用ASP.net,我期待这个对象传递给通过jQuery一个页面的方法。

I'm using ASP.net and I'm looking to pass this object to a page method via jquery.

到目前为止,我有这个JavaScript code:

So far, I have this javascript code:

function LoadObject () {

  var TheObject = MyObj.toString();

  $.ajax({
    type: "POST",
    url: "../Pages/TestPage.aspx/GetCount",
    data: TheObject,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successFn,
    error: errorFn
    });
};

我有一个页面的方法在cs文件设置和我把一个断点,但它永远不会出现;什么也没有发生。

I have a page method set up in the .cs file and I put a breakpoint in it but it never gets there; nothing happens.

请让我知道什么样的变化我需要得到这个工作。
谢谢你。

Please let me know what changes I need to make to get this to work. Thanks.

推荐答案

您需要序列TheObject成JSON字符串,并确保利用getCount方法接受具有相同签名作为TheObject的对象。

You need to serialize TheObject into a JSON string, and ensure that the GetCount method accepts an object with the same signature as TheObject.

我用jQuery.JSON库要做到这一点,使我的语法就变成了:

I use the jQuery.JSON library to do this so that my syntax becomes:

data: "{ methodParameterName: " + $.toJSON(TheObject) + " }"

我用这个库的,但你可以acheive同样的事情用类似的方式的任何其他文库

I use this library, but you can acheive the same thing with any other library in a similar manner

这篇关于对象变量转换成JSON字符串asp.net页面的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆