0

I have a document containing valid JSON content. It's saved on my machine as filename.json

I have a web page using that uses and ajax post to call a method that will return a json object.

** Note, I probably dont need to Post, I'm not actually using the SearchObject or the Id I'm passing, this was cut an past from another page, just trying to get it to work before cleaning it up.

The method my ajax is calling:

        [WebMethod]
        [AllowAnonymous]
        public JsonResult GetJson(SearchObject model)
        {

            string text = System.IO.File.ReadAllText(@"C:\Users\mroberts\Desktop\filename.json");
            return Json(text);
        }

This is working, it returns data in the following format:

 [{"NAME":"xxx  xxxx",
   "DIRECT PHONE":"(xxx) xxx-xxxx",
   "EMAIL":"xxxxxx@xxxxxxxxx.com",
   "JOB TITLE":"xxxxxx",
   "DEPARTMENT":"xxxxxx"}]

My Jquery/Ajax:

$(document).ready(function () {
        // Add the page method call as an onclick handler for the div.
        $("a#BigButton").click(function (e) {
            e.preventDefault();
            var a = $(this).attr("id");

            var data = {
                Id: a
            }
            $.ajax({
                //url: "../Controllers/Home/GetTier",
                url: "/Home/GetJson",
                type: "POST",
                data: data,
                context: this,
                error: function () { },
                dataType: 'json',
                success: function (response) {
                    console.log(response);
                    var loop = $(response).size();
                    for (var i = 0; i < loop + 1; i++) {
                        console.log(response);

                    }
                }
            });
        });
    });

However, when I return this, I am logging it to my console, and am getting an error: Error: Syntax error, unrecognized expression: my json result....

Im not sure why this is invalid. I validated I've reviewed my result, and used online validation to verify the structure of my json, and it appears to be fine.

Am I overlooking something?

Is it the "return Json" that is breaking things?

Mark
  • 4,773
  • 8
  • 53
  • 91
  • 3
    Where are you getting the error? But if you string is *already* JSON, then you don't need to turn it into JSON. – Matt Burland Jan 27 '15 at 14:31
  • Can I just return it as a string? Or should I create a list first, and return a list? – Mark Jan 27 '15 at 14:32
  • I'm getting the error in my console when I log the response. – Mark Jan 27 '15 at 14:32
  • you mean when you console.log the response in your browser? try `console.log("%o", response);` – Brett Caswell Jan 27 '15 at 14:33
  • 1
    @Mark JSON is a plain text format, just like HTML. If your file is JSON, you don't need to turn it into JSON *again*, that's just like HTML-encoding and HTML file would lead to broken output. – Tomalak Jan 27 '15 at 14:34
  • What is `$(response)` supposed to do here? – DavidG Jan 27 '15 at 14:35
  • @BrettCaswell , the %o, worked, what did that do exactly? – Mark Jan 27 '15 at 14:35
  • it depends on your browser, but `%o` will log an object explicitly. https://developer.chrome.com/devtools/docs/console-api .. it would seem that `console.log(response)` is attempting to parse your `response`. – Brett Caswell Jan 27 '15 at 14:38
  • Try `$.each(response, function(i, item) { console.log(item); });` in your success callback. – MrUpsidown Jan 27 '15 at 14:39
  • @MattBurland, the problem / solution may be related, but it certainly isn't a duplicate.. – Brett Caswell Jan 27 '15 at 14:44
  • 1
    @BrettCaswell It is a duplicated. `Json(text)` turns `text` into JSON. it does not care whether `text` already contains JSON, it will simply encode it *again*. Simply returning the raw file content is one way to solve this. (A better way might be to to a parse-and-encode pass on the server to rule out the possibility that the client ever receives broken JSON, even if the file itself became invalid) – Tomalak Jan 27 '15 at 14:45
  • @Tomalak, to draw you a comparison: questions are like exceptions.. they are raised.. this question inherits the other one, and they have the same set of solutions/handling (mostly).. but people are more likely to raise THIS question. based on these flags, yall seem to think `System.Data.SqlClient.SqlException` is `System.Data.Common.DbException` – Brett Caswell Jan 27 '15 at 15:26
  • @Mark Given your lack of further comments I take it that the duplicate thread has in fact helped you? – Tomalak Jan 27 '15 at 15:40

0 Answers0