Question

I am working on a C# console application using the Nancy Framework and the Spark view engine, and I am trying to replicate something from another project. However, I am very inexperienced with both Javascript and JSON. To call a chat function in my C# code from my HTML, right now I simply use something like the following...

HTML:

http://localhost:1234/sendchat?message="this is a test message"

C# Code:

    Get["/sendchat"] = x =>
    {
        string message = Request.Query.message;
        string message2 = message.Replace("\"", "");
        Console.WriteLine(message2);

        return View["console.spark"];
    };

The problem is that this causes the page to reload. In the project I am looking at for reference, they use Javascript/JSON to call the same type of function without doing a page reload. I understand all of it except for the JSON line as I don't understand what the DataSource is...

$(document).ready(function () {
    $("#typechat").keypress(function (event) {
        if (event.keyCode == '13') {
            event.preventDefault();
            message = escape($("#typechat").attr('value'));
            $.getJSON(dataSource + "?req=sendchat&message=" + message);
            $("#typechat").attr('value', "");
        }
    });
});
Was it helpful?

Solution

dataSource is just an http domain like http://yourserver.com/possibly/with/a/path. It'll be a string defined somewhere in the code.

JSON resources are fetched just like regular HTML pages, with a normal GET request over HTTP. The only difference is the content is JSON not HTML. Try this in your browser for example to see the JSON returned by the SO api:

http://api.stackoverflow.com/1.1/users/183579

(If you don't have a browser plugin to format/highlight JSON nicely it might just look like a long messy string)

OTHER TIPS

Data source is propobly some web page

dataSource = "http://somepage.com/someaction";

wich renders response as json text, response is grabbed and then parsed to javascript object

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top