开发者

How do I do a simple ajax post with ScriptSharp 0.6 (C# to Javascript)?

开发者 https://www.devze.com 2023-01-05 03:22 出处:网络
ScriptSharp 0.6 turns C# to Javascript and looks pretty neat. However I don\'t have much experience with event handling in C# and I\'m stuck on how to do this:

ScriptSharp 0.6 turns C# to Javascript and looks pretty neat. However I don't have much experience with event handling in C# and I'm stuck on how to do this:

$.post("urlToPostTo", $("#testform").Serialize(),
   function(data){
     alert(data.name); // Something sent from the server
   }, "json");

That's a simple JQuery form post that returns an alert with the JSON return object as the success callback. For ScriptSharp it is setup like:

public static XmlHttpRequest Post(string url, object data, AjaxCallback callback);

With AjaxCallback defined as:

public d开发者_JS百科elegate void AjaxCallback(object data);

It seems like I'd set something up along these lines:

JQuery.Post("urlToPostTo", JQuery.Select("#testform"), callbackhere);

I'm at a loss as to how to setup and define the callback in C#, any ideas?


Your code would be something like this:

JQuery.Post("url", JQuery.Select("#testform").Serialize(),
    delegate(object data) {
        Script.Alert(Type.GetField(data, "name"));
    });

Basically an anonymous delegate in c# gets translated to a function suitable for use as a callback. You can also write it as a regular method, and use a delegate to that method.

Hope that helps.

0

精彩评论

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