【问题标题】:Ajax call to aspx page JQuery JSONAjax 调用 aspx 页面 JQuery JSON
【发布时间】:2009-06-04 21:35:27
【问题描述】:

我正在尝试对 aspx 页面进行 Ajax 调用 在服务器端请求以“对象对象”的形式出现,我无法将它序列化为“不是 JSON 原语..”,但是当我传递 json 字符串而不是 json 对象时,它可以工作......问题是在客户端我' m 使用我必须在发送之前将其转换为字符串的 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);

【问题讨论】:

    标签: c# asp.net jquery json


    【解决方案1】:

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

    JSON.stringify(myjson)
    

    ...而不是...

    myjson.toJSONString()
    

    我将它与 jQuery 一起使用没有问题,但请注意,我使用的是来自 www.json.org 的 json2.js。我不知道你用的是不是这个。

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      相关资源
      最近更新 更多