ajax传值

huitian1621 2013-05-29 11:59:56

<select name="ddladdr" id="ddladdr" style="width: 100px; margin-left:20px;" onchange="alladdr()">
<option value="0">所有地区</option>
<option value="1">北京</option>
<option value="2">上海</option>
<option value="3">天津</option>
...
</select>

function alladdr(){
var sarea = document.getElementById("ddladdr").value
$.ajax({
type: 'POST',
url: 'DataServer.ashx',
data: sarea,
});
}


DataServer.ashx:



public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string strarea = context.Request.Form["sarea"].ToString();
string areadata = getAreaData(strarea);
context.Response.Write(areadata);
}

public string getAreaData(string areadata)
{
...
}


这样写可以吗? 怎么把select里改变的值用ajax传递到DataServer中呢?
...全文
116 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
人生导师 2013-05-30
  • 打赏
  • 举报
回复
你可以用JS注册SelectChange事件,然后获取通过下面的方式获得选中的值:
var selectedValue=ddar.options[ddar.selectedIndex].text;    //获取选中的值
然后再SelectChange事件里面调用你的这个代码就可以:
function alladdr(){
var ddar =document.getElementById("ddladdr");
var  sarea = ddar.options[ddar.selectedIndex].text
         $.ajax({
                type: 'POST',
                url: 'DataServer.ashx',
                data: sarea,
            });
}
这样就可以每次发生选择事件之后就可以传递值到DataServer.ashx中处理了
u010853793 2013-05-30
  • 打赏
  • 举报
回复
引用 3 楼 findcaiyzh 的回复:
这个可能更简单点,不需要构造Json字符串了。 string json = new StreamReader(context.Request.InputStream).ReadToEnd(); 也是来自 http://stackoverflow.com/questions/2948628/asp-net-passing-json-from-jquery-to-ashx
现在才知道还有这种方法可以实现,学习了
crazyleo814 2013-05-30
  • 打赏
  • 举报
回复
引用 3 楼 findcaiyzh 的回复:
这个可能更简单点,不需要构造Json字符串了。 string json = new StreamReader(context.Request.InputStream).ReadToEnd(); 也是来自 http://stackoverflow.com/questions/2948628/asp-net-passing-json-from-jquery-to-ashx
然后再转换为你后台定义好的任何Model都行了
宝_爸 2013-05-30
  • 打赏
  • 举报
回复
这个可能更简单点,不需要构造Json字符串了。 string json = new StreamReader(context.Request.InputStream).ReadToEnd(); 也是来自 http://stackoverflow.com/questions/2948628/asp-net-passing-json-from-jquery-to-ashx
宝_爸 2013-05-30
  • 打赏
  • 举报
回复

 $.ajax({
            type: "POST",
            url: "handler.ashx",
            data: { firstName: 'stack', lastName: 'overflow' },
            // DO NOT SET CONTENT TYPE to json
            // contentType: "application/json; charset=utf-8", 
            // DataType needs to stay, otherwise the response object
            // will be treated as a single string
            dataType: "json",
            success: function (response) {
                alert(response.d);
            }
        });

using System;
    using System.Web;
    using Newtonsoft.Json;

    public class Handler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string myName = context.Request.Form["firstName"];

            // simulate Microsoft XSS protection
            var wrapper = new { d = myName };
            context.Response.Write(JsonConvert.SerializeObject(wrapper));
        }

        public bool IsReusable
        {
           get
           {
                return false;
           }
        }
    }
代码来自:

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧