用Ajax application / json POST数据,用ashx文件读取

时间:2014-05-08 08:09:30

标签: .net json stringify

  $(document).ready(function () {
        $('#dropDownMenu1').change(function () {
            var customer = { contact_email: "s.selcuk@hotmail.com", company_password: "123" };
            $.ajax({
                type: "POST",
                data: JSON.stringify(customer),
                url: "webServices/apiDeneme.ashx",
                contentType: "application/json"
            });
        });
    });

我将此客户变量以这种方式发送到apiDeneme.ashx。我把调试poing,请求来到apiDeneme.ashx但这个电子邮件字符串变量等于null。 ? (下同)

public void ProcessRequest(HttpContext context)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    string email = context.Request.Form["contact_email"];      

    sb.Append("[");
    sb.Append("{");
    sb.Append("\"Sonuc\":\"" + email + "\"}]");

    context.Response.ContentType = "application/json";
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.Write(sb.ToString());
    context.Response.End();
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

顺便说一下,另一个问题是,这里的登录过程是否可靠?谢谢!

1 个答案:

答案 0 :(得分:0)

尝试以下代码: -

$(document).ready(function () {
        $('#dropDownMenu1').change(function () {
            var customer = { contact_email: "s.selcuk@hotmail.com", company_password: "123" };
            $.ajax({
                type: "POST",
                data: {context : JSON.stringify(customer)},
                url: "webServices/apiDeneme.ashx",
                contentType: "application/json",
                success : function(result) {
                         alert("called");
               }
            });
        });
    });

并添加[httpost],如下所示: -

[HttpPost]
public void ProcessRequest(HttpContext context)
{
}