Ajax调用aspx页面JQuery JSON

时间:2009-06-04 21:35:27

标签: c# asp.net jquery json

我正在尝试对aspx页面进行Ajax调用 在服务器端请求来自“对象对象”,我不能序列化它得到“不是一个JSON原语..”然而它工作,而不是json对象我传递json字符串....问题是在客户端我'使用json对象,我必须在发送之前将其转换为字符串。我尝试使用json.org中的函数JSONToString(),但是当我添加jquery库时会抛出一个错误。  有谁知道应该怎么做我非常感谢任何帮助。

添加“JQuery / jquery-1.3.2.js”

添加“js / json.js”

<script type="text/javascript">
    function callAjax() {

    var myjson = { document: { manufacture: { item: ['Alfa Romeo']}} }     
        $.ajax({
            url: 'jsonresponse.aspx',
            type: 'POST',                
            //contentType: "application/json; charset=utf-8",
            data: myjson.toJSONString(),  // throws an error in json libary //  return JSON.parse(this, filter); //.. Microsoft JScript compilation error: Syntax error
            //data: myjson, can't serialize on the server request comes as object object
            //data:{ document: { manufacture: { item: ['Alfa Romeo']}} }, works but I need something to convert object to a string as it is much bigger then the one in example 
            timeout: 1000000,
            dataType: "json",
            error: function() {
                alert("error");
            },
            success: function(myResult) {
            //alert(myResult);              
            }
        });
    }


jsonresponse.aspx

    XmlDocument myxml = new XmlDocument();  
    StreamReader reader = new StreamReader(Page.Request.InputStream);

    string test;
    test = reader.ReadToEnd();

    JavaScriptSerializer jss = new JavaScriptSerializer();
    myxml = jss.Deserialize<XmlDocument>(test);

1 个答案:

答案 0 :(得分:2)

我不确定为什么在添加jQuery之后会抛出错误,但也许会尝试...

JSON.stringify(myjson)

......而不是......

myjson.toJSONString()

我正在使用jQuery而没有问题,但请注意,我正在使用www.json.org上的json2.js。我不知道你是否正在使用它。