编写和发送JSON文件从asp

本文关键字:文件 asp JSON | 更新日期: 2023-09-27 18:05:55

只是一个免责声明:我绝不是一个c#/.net程序员。

我想在查询字符串中发送一个json对象到。aspx页面(AJAX),然后在客户端自动下载该文件(jQuery或JS)。

这是我的当前文件upload.aspx

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8"%>
<script runat="server">
    public void Page_Load(object sender, EventArgs e) {
        string json = "[{'"Id'":'"1'",'"name'": '"Test 1'"},{'"Id'":'"2'",'"name'": '"Test 2'"}]";
        Response.Clear(); 
        Response.ContentType = "application/json; charset=utf-8"; 
        Response.Write(json); 
        Response.End();              
    }  
</script>

我怎样才能使它工作?

另外,作为一个侧面的事情,是否有可能将此作为自定义文件扩展名发送(在修改服务器配置上的MIME类型之后)。

编写和发送JSON文件从asp

Use Like

Jquery

jQuery.ajax({
    type: "POST",
    url: "Login.aspx/checkUserNameAvail",
    contentType: "application/json; charset=utf-8",
    data: "{'iuser':'" + userid + "'}",
    dataType: "xml",
    success: function (msg) {
        $(msg).find("Table").each(function () {
            var username = $(this).find('UserName').text();
            if (username != '') {
                //window.location.replace('/iCalendar.aspx');
                alert('This username already taken..');
                $("#reguser").val('');
                $("#reguser").focus();
            }
            else {
            }
        });
    },
    error: function (d) {
    }
});

cs

[WebMethod(enableSession: true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public static string checkUserNameAvail(string iuser)
    {
        try
        {
            iCalendarClass iC = new iCalendarClass();
            DataSet ds = iC.checkUserNameAvail(iuser);
            return (ds.GetXml());
        }
        catch
        {
            return null;
        }
    }