Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1838026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:25:39+00:00 2026-05-17T06:25:39+00:00

I’m aiming to build up a JSON array of mouse positions and pass them

  • 0

I’m aiming to build up a JSON array of mouse positions and pass them to a controller. For some reason my json is returning from the controller as undefined can anyone spot my problem?

// Attempt at declaring a JSON object
var personResults = { "answers": [] };

// Onclick I fire in values from mouse event
personResults.answers[count] = { "xpos": mouseX, "ypos": mouseY };

// AJAX Call
$.ajax({
    url: "Save",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(personResults),
    success: function (data) { alert(data.toSource()); },
    error: function (req, status, error) {
        alert("error! " + status +    error + req);
    }
});

I then receive the jsontext from my .NET MVC Controller:

public JsonResult Save(string personResults)
{
    return Json(personResults);
}

As you can see the response back to the AJAX should just be the same json I sent to it – but I’m receiving undefined values back from the server even though my json seems to be constructing Ok and I’ve tested it – it’s valid.

If I set Save to receive of type “string” I receive this alert “(new String(“”))”; if i set the save action to receive of type “JsonResult” I receive this alert:

({ContentEncoding:null, ContentType:null, Data:null, JsonRequestBehavior:1})

Am I missing something totally obvious? I just want to check that my json is being sent successfully to the controller so I can manipulate it later!

Here’s the format of my JSON:

{"answers":[
    {"xpos":293,"ypos":415},{"xpos":293,"ypos":415},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
    {"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416}
]}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:25:39+00:00Added an answer on May 17, 2026 at 6:25 am

    How I understand your question you want to know how to send data (input parameters) to the action of your MVC controller which will return JsonResult back.

    Your first error is that input data of the action Save should be not in JSON format. MVC in designed more to work with forms, so if you send any data to the Save action the data must be url-encoded. For example to have “Bla Bla” as input of personResults parameter of the action Save your jQuery call should be following

    $.ajax({
        url: "Home/Save",
        type: "POST",
        data: "personResults=Bla+Bla",
        success: function (data) {
            alert(data); },
        error: function (req, status, error) {
            alert("error! " + status + error );
        }
    });
    

    More better not make any manual encoding of the string “Bla Bla” and use data: {personResults: "Bla Bla"} instead of data: "personResults=Bla+Bla".

    If you want to send more complex data to the MVC controller you can do this but you have to convert the data to JSON and back manually. For example if you include a new action Save1:

    public JsonResult Save1 (string encodedAnswers) {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        MyData data = serializer.Deserialize<MyData> (encodedAnswers);
        return Json (data);
    }
    

    where MyData defined as

    public class MyData
    {
        public List<MyPosition> answers { get; set; }
    }
    public class MyPosition
    {
        public int xpos { get; set; }
        public int ypos { get; set; }
    }
    

    (In the same way you can use DataContractJsonSerializer instead of JavaScriptSerializer.)
    The corresponding JavaScript code could be following

    var personResults = {"answers":[{"xpos":293, "ypos":415 },{"xpos":293, "ypos":416}]};
    // or var personResults = {answers: [{xpos: 293, ypos: 415},{xpos: 293, ypos: 416}]};
    
    $.ajax({
        url: "Home/Save1",
        type: "POST",
        data: {encodedAnswers: JSON.stringify(personResults)},
        success: function (data) {
            alert("answers[0].xpos="+data.answers[0].xpos); },
        error: function (req, status, error) {
            alert("error! " + status +    error );
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.