开发者

What is a JSON Data Source?

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($("#typ开发者_如何转开发echat").attr('value'));
            $.getJSON(dataSource + "?req=sendchat&message=" + message);
            $("#typechat").attr('value', "");
        }
    });
});


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜