C# Ajax 调用 aspx 页面 JQuery JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/953243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:08:19  来源:igfitidea点击:

Ajax call to aspx page JQuery JSON

c#asp.netjqueryjson

提问by

I ‘m trying to make an Ajax call to an aspx page On the server side request comes as "object object" and I cannot serialize it getting “not a JSON primitive.. ” it works however when instead of json object I pass json string .... Problem is that on a client side I'm using a json object that I have to convert to string before sending. I tried using function JSONToString() from json.org but this throws an error when I add jquery library. Does anybody know how it should be done I would much appreciate any help.

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

add "JQuery/jquery-1.3.2.js"

添加“JQuery/jquery-1.3.2.js”

add "js/json.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


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);

采纳答案by Casey Williams

I'm not sure why it's throwing an error after adding jQuery, but maybe try...

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

JSON.stringify(myjson)

...instead of...

...代替...

myjson.toJSONString()

I'm using this with jQuery without issue, but note, I'm using json2.js from www.json.org. I can't tell if that's what you're using or not.

我在 jQuery 中使用它没有问题,但请注意,我使用的是来自 www.json.org 的 json2.js。我不知道这是否是您正在使用的。