Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to call the server side from JavaScript and then pass a string array back to JavaScript, but running into problems.

// Call the server-side to get the data.
$.ajax({"url" : "MyWebpage.aspx/GetData",
        "type" : "post",
        "data" : {"IdData" : IdData},
        "dataType" : "json",
        "success": function (data)
        {
            // Get the data.
            var responseArray = JSON.parse(data.response);

            // Extract the header and body components.
            var strHeader = responseArray[0];
            var strBody = responseArray[1];

            // Set the data on the form.
            document.getElementById("divHeader").innerHTML = strHeader;
            document.getElementById("divBody").innerHTML = strBody;
        }
});

On the ASP.Net server side, I have:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static object GetTip(String IdTip)
{
    int iIdTip = -1;
    String[] MyData = new String[2];


    // Formulate the respnse.
    MyData[0] = "My header";
    MyData[1] = "My body";

    // Create a JSON object to create the response in the format needed.
    JavaScriptSerializer oJss = new JavaScriptSerializer();

    // Create the JSON response.
    String strResponse = oJss.Serialize(MyData);

    return strResponse;
}

I am probably mixing things up, as I am still new to JSON.

UPDATE with error code:

Exception was thrown at line 2, column 10807 in     http://localhost:49928/Scripts/js/jquery-1.7.2.min.js

0x800a03f6 - JavaScript runtime error: Invalid character

Stack trace: parse JSON[jquery-1.7.2.min.js] Line 2

What is my problem?

share|improve this question
3  
what is the error that you are getting? – PSL Apr 17 at 23:51
Need more input... – Lee Taylor Apr 17 at 23:57
See my edit in the question. – Sarah Weinberger Apr 17 at 23:58
Why the deduction in points? – Sarah Weinberger Apr 18 at 0:07
1  
@SarahWeinberger do not know who down voted you but try to put as much information as possible in your question otherwise you will end up getting thumbs down :) – PSL Apr 18 at 0:34
show 2 more comments

2 Answers

up vote 0 down vote accepted

I modified your ajax call script to :

// Call the server-side to get the data.
$.ajax({
    url: "WebForm4.aspx/GetTip",
    type: "post",
    data: JSON.stringify({ IdTip: "0" }),
    dataType: "json",
    contentType: 'application/json',
    success: function (data) {
        // Get the data.
        var responseArray = JSON.parse(data.d);

        // Extract the header and body components.
        var strHeader = responseArray[0];
        var strBody = responseArray[1];

        // Set the data on the form.
        document.getElementById("divHeader").innerHTML = strHeader;
        document.getElementById("divBody").innerHTML = strBody;
    }
});

Note that I added contentType: 'application/json' and changed

var responseArray = JSON.parse(data.response);

to

var responseArray = JSON.parse(data.d);
share|improve this answer
Ajakblackgoat's response was the answer with one change. I had to change 'IdTip: "0"' to 'IdTip: IdTip', as the JavaScript variable IdTip had the primary key. It is interesting that the first argument did not need quotes around it to indicate the parameter. (continued second comment) – Sarah Weinberger Apr 18 at 14:35
I did not realize at the beginning, but I never hit a breakpoint in the C# code. I honestly thought that the code executed and just that Visual Studio was being obnoxious. The same was true for JavaScript inside the success code. Further debugging this morning yielded that the b variable was my HTML document code that JSON tried to parse. I received one crash every time the $.ajax processed after returning from the JavaScript procedure, hence no stack trace. Ajakblackgoat nailed the problem, not that I understand the why on his changes. Both were needed. Thanks! – Sarah Weinberger Apr 18 at 14:38
JSON.stringify will take care of converting the parameter names to string. And don't forget the contentType must be set to 'application/json' – ajakblackgoat Apr 18 at 15:03

This s purely out of guess work. But see if this is what you are getting:- In your Ajax call, your data type is json and looking at the method you are returning a json string. So you do not need to do JSON.parse(data.response). Instead just see if the below works for you. Also i dont see a response object in your Json, instead it is just an array. So it must be trying to parse undefined

 var strHeader = data[0];
 var strBody = data[1];
share|improve this answer
After implementing ajakblackgoat's code, I started receiving success messages. I guess I should suspect that if I do not receive a success message or hit a breakpoint, then I am really not getting there and the problem lies elsewhere. Nonetheless, I decided to see what data[0] and data[1] returns. They returned "undefined". I placed alert statements to check and also hit the breakpoint. Both showed "undefined". I placed alert("data[0]"+data[0]) statement. When I did the same using ajakblackgoat's parsed data, I got the proper value returning. – Sarah Weinberger Apr 18 at 14:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.