LoginSignup
12
13

More than 5 years have passed since last update.

Classic ASPでAjax

Last updated at Posted at 2015-12-28

昔やってたClassic ASPの案件を思い出してるうち、「あーあれAjaxでやれたらもっとシンプルになったんじゃないかなー」などと思ってしまい、Classic ASPでAjaxをやる方法を調べた。

フロントサイドのコード

index.aspにアクセスしたらHTMLとJSを出力します。

index.asp
<%@ LANGUAGE="VBScript" %>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="shift_jis">
    <title>Document</title>
</head>
<body>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="./index.js"></script>
</body>
</html>

index.jsからjQueryのajaxを使ってGETを投げています。POSTも同様にできるかと思います。

index.js

(function(){
    $ = window.$;

    $.ajax({
        type:"GET",
        url: "ajax.asp",
        dataType: "json",
        data: {name: "Hoge"}
    }).done(function(msg){
        document.writeln(Object.keys(msg) + ": " + msg.name);
    });
}());

サーバーサイドのコード

Response.WriteでJSONを出力します。
なお、JSONは文字列を整形して無理やりJSON形式にするしかなさそうです。
POSTのときはRequest.QueryStringではなくRequest.Formになります。

ajax.asp
<%@ LANGUAGE="VBScript" %>
<%
    Response.ContentType = "application/json"
    Dim ret
    If(Request.QueryString("name") = "Hoge") then
        ret = "{""name"": ""Huga""}"
    End If
    Response.Write(ret)
%>

まとめ

Classic ASPでのAjaxの実現方法でした。使う機会はおそらくないでしょう。

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13