我在 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应用程序打开浏览器,我手动输入参数(对于谷歌地图来说是长和纬度)浏览器接受参数并显示 map 位置,当我尝试使用上面的代码发送浏览器不执行任何操作,有任何帮助可以使用 delphi 发布参数并且服务器使用参数执行 url 吗?
代码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 监听参数?
最佳答案
您的 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;
关于javascript - 使用Delphi通过Internet将参数发布到asp网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39319041/