views:

3139

answers:

1

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.

add "JQuery/jquery-1.3.2.js"

add "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  A: 

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

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.

Lunchy
Works perfect!!! Thanks a lot!
Excellent, glad it's workin.
Lunchy