[FluorineFx]用ASP.NET直接輸出JavaScript API類別庫服務

  • 3445
  • 0

[FluorineFx]用ASP.NET直接輸出JavaScript API類別庫服務

首先先下載FluorineFx.NET至專案中

1.在專案跟目錄建立做為接口的空白網頁JsonGateway.aspx

2.下載json2.min.rar將json2.js加入

3.在App_Code中撰寫服務的類別程式,如下範例MyService.cs

namespace ServiceLibrary
{
 [RemotingService()]
 public class MyService
 {
   public MyService()
   {
   }
  
   [FluorineFx.Json.JsonRpcMethod]
   public string Echo(string txt)
   {
     return txt;
   }
 }
}

4.建立MyService.js,以合併json2.js與MyService的引用載入

(function () {
    function importJS(src, look_for, onload) {
        var s = document.createElement('script');
        s.setAttribute('type', 'text/javascript');
        s.setAttribute('src', src);
        if (onload)
            wait_for_script_load(look_for, onload);
        if (eval("typeof " + look_for) == 'undefined') {
            var head = document.getElementsByTagName('head')[0];
            if (head) head.appendChild(s); else document.body.appendChild(s);
        }
    }
    function wait_for_script_load(look_for, callback) {
        var interval = setInterval(function () { if (eval("typeof " + look_for) != 'undefined') { clearInterval(interval); callback(); } }, 50);
    }
    function basePath() {
        var scriptSrc = document.getElementsByTagName('script')[document.getElementsByTagName('script').length - 1].src;
        var jsName = scriptSrc.split('/')[scriptSrc.split('/').length - 1];
        return scriptSrc.replace(jsName, '');
    }
    var root_url = basePath();
    importJS(root_url + "JS/json2.min.js", "json2", function () {
    });
    importJS(root_url + "JsonGateway.aspx?proxy&destination=fluorine&source=ServiceLibrary.MyService", "MyService", function () {
    });
})();

5.可於前端客戶網站使用服務了