delphi webbrowser 跨域访问
时间: 2023-07-07 15:14:36 浏览: 50
在 Delphi 中使用 WebBrowser 访问跨域网页需要在代码中添加以下语句:
```delphi
uses
MSHTML;
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
var
Doc: IHTMLDocument2;
Win: IHTMLWindow2;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Win := Doc.parentWindow;
Win.execScript('document.domain = "example.com";', 'JavaScript');
end;
```
其中,`example.com` 为你所访问的网页所在的域名。这段代码将通过 JavaScript 修改文档的域名,以实现跨域访问。
相关问题
delphi webbrowser json 跨域
在Delphi中使用WebBrowser控件进行跨域请求,需要通过JavaScript来实现。具体步骤如下:
1. 创建一个新的HTML文件,将需要请求的数据以JSON格式嵌入其中。
2. 在HTML文件中添加一个JavaScript函数,用于将请求数据发送到服务器并接收响应。
3. 在Delphi应用程序中加载该HTML文件,并通过WebBrowser控件调用该JavaScript函数。
以下是一个示例代码,用于实现以上步骤:
HTML文件(data.html):
```html
<!DOCTYPE html>
<html>
<head>
<title>JSON Data</title>
<meta charset="UTF-8">
<script type="text/javascript">
function getData() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
window.external.onDataReceived(data);
}
};
xmlhttp.open("GET", "http://example.com/data.json", true);
xmlhttp.send();
}
</script>
</head>
<body onload="getData()">
</body>
</html>
```
Delphi代码:
```delphi
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
begin
if URL = 'file:///C:/data.html' then begin
WebBrowser1.OleObject.Document.parentWindow.execScript('getData();', 'javascript');
end;
end;
procedure TForm1.OnDataReceived(const data: WideString);
begin
ShowMessage(data);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('file:///C:/data.html');
end;
```
在该示例代码中,当WebBrowser控件加载完毕data.html文件时,会调用JavaScript函数getData(),该函数会向服务器发送请求并接收响应。当响应数据返回时,通过调用window.external.onDataReceived()函数将数据传递给Delphi应用程序。在Delphi应用程序中,实现了一个名为OnDataReceived的回调函数来处理接收到的数据。
delphi webbrowser json
Delphi WebBrowser component does not have built-in support for JSON parsing. However, you can use a third-party JSON library to parse JSON data in Delphi.
One popular JSON library for Delphi is SuperObject. Here's an example of how you can use SuperObject to parse JSON data in a Delphi WebBrowser component:
First, add the SuperObject unit to your project by adding the following line to your uses clause:
```Delphi
uses
SuperObject;
```
Then, you can use the following code to parse a JSON string and extract some data:
```Delphi
var
Doc: OleVariant;
JSONData: string;
SO: ISuperObject;
begin
Doc := WebBrowser1.Document;
JSONData := Doc.body.innerHTML; // assuming the JSON data is in the HTML body
SO := TSuperObject.ParseString(PWideChar(WideString(JSONData)), True);
if Assigned(SO) then
begin
// access the JSON data using the SO object
ShowMessage(SO.AsObject.S['name']);
end;
end;
```
This code assumes that the JSON data is stored in the HTML body of the WebBrowser component. You can modify it to suit your specific needs.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)