我在Delphi中制作了这段代码
TForm10.Button1Click(Sender: TObject); var Response: TStringStream; Params: TStringList;
begin
Params := TStringList.Create;
try
Params.Add('redirect=http://localhost:1645/default.aspx');
Params.Add('long=' + edit1.Text);
Params.Add('lat=' + edit2.Text);
Response := TStringStream.Create('');
try
IdHTTP1.AllowCookies := True;
IdHTTP1.HandleRedirects := True;
IdHTTP1.Post('http://localhost:1645/default.aspx', Params, Response);
memo1.Text := Response.DataString;
except
on E: Exception do
begin
showmessage('Error: ' + E.Message);
end;
end;
end;
end;
要将参数发送到aspx页面,当我在VS和IIS中运行web应用程序打开浏览器时,我会手动输入参数(它是长的和谷歌地图的lat)浏览器接受参数并显示地图位置,我尝试发送上面的代码浏览器没有做任何事情,任何帮助发布参数与delphi和服务器执行URL与params?
代码c#asp
public partial class YourFirstGoogleMap : System.Web.UI.Page
{
public string v;
public string c;
protected void Page_Load(object sender, EventArgs e)
{
v = Request.QueryString["long"];
c = Request.QueryString["lat"];
}
}
和aspx
<script type ="text/javascript">
function InitializeMap()
{
var latlng = new google.maps.LatLng(<%=v%>, <%=c%>);
var myOptions = {
zoom: 3,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
}
window.onload = InitializeMap;
</script>`
如何让javascript听取参数?
答案 0 :(得分:3)
您的ASP C#代码期望参数在URL查询字符串中发送,但您的Delphi代码正在POST
正文中发送它们。正确的Delphi代码看起来更像是这样:
procedure TForm10.Button1Click(Sender: TObject);
begin
try
IdHTTP1.AllowCookies := True;
IdHTTP1.HandleRedirects := True;
Memo1.Text := IdHTTP1.Get('http://localhost:1645/default.aspx?redirect=' + TIdURI.ParamsEncode('http://localhost:1645/default.aspx') + '&long=' + TIdURI.ParamsEncode(edit1.Text) + '&lat=' + TIdURI.ParamsEncode(edit2.Text));
except
on E: Exception do
begin
ShowMessage('Error: ' + E.Message);
end;
end;
end;