简体   繁体   中英

Cant pass data JQuery $.ajax to ashx handler

This is a part of my AJAX code:

    $.ajax({
        type: "POST",
        url: "ajax.ashx?method=LoadCities",
        data: "{state_id:'" + state_id + "'}",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
.
.
.

and this is a part of my ASHX handler code:

public class ajax : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        context.Response.ContentType = "application/json;charset=utf-8";

        string method = context.Request["method"];
        if (method == "LoadCities")
        {
            object ss = context.Request.Form["state_id"];
            context.Response.Write(LoadCities(ss));
        }
    }

I can't get "state_id" and it is always null How can I get "state_id"

You can pass it as query string,

$.ajax({
    type: "POST",
    url: "ajax.ashx?method=LoadCities&state_id=" + state_id,
    contentType: "application/json;charset=utf-8",
    dataType: "json",

. . .

and get your state_id in your .ashx handler,

string lsStateId = System.Convert.ToString(context.Request.QueryString["state_id"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM