日期:2014-05-17  浏览次数:20423 次

ajax从javascript传参至asp.net,接收不到变量
HTML code

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="location.aspx.cs" Inherits="WebRole1.location" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        var xmlHttp;
        function createXMLHttpRequest() {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
            else {
                alert("your browser doesn't support AJAX");
            }
        }
        function startRequest(url){
            createXMLHttpRequest();
            xmlHttp.open("GET", url, true);
            xmlHttp.onreadystatechange =callBack ;
            xmlHttp.send(null);
        }
        function callBack() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var res = xmlHttp.responseText;
                    window.alert(res);
                } else {
                    window.alert("error");
                }
            }
        }  
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(getPostion);
            }
        }
        function getPostion(position) {
            var lat = position.coords.latitude;
            var lon = position.coords.longitude;
            var url = "location.aspx?lat=" + lat + "&lon=" + lon;
            startRequest(url);
        }
    </script>

    <asp:TextBox ID="text" runat="server" CssClass="text" MaxLength="100" Columns="40" TextMode="MultiLine"></asp:TextBox>
    <asp:Button ID="textSubmit" runat="server" CssClass="textSubmit" Text="submit" OnClientClick="getLocation()" OnClick="Post"/>
 
    <hr />
</asp:Content>

[code=C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebRole1
{
    public partial class location : System.Web.UI.Page
    {
        protected void Post(object sender, EventArgs e)
        {
            Response.Write("<script>alert(" + Request.QueryString["lat"] + "); </script>");
        }
    }
}



alert出来的值一直是undifined,求解啊!~ 在线等



[/code]

------解决方案--------------------
protected void Post(object sender, EventArgs e)
{
Response.Write("<script>alert(" + Request.QueryString["lat"] + "); </script>");
}
改成

 protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Write("Request.QueryString["lat"]);
Response.End();
}