# 2.4 關於跨網域

## 關於跨網域

預設上都是禁止跨網域取得資料的，若跨網域取得資料，會發現如下錯誤：

![](/files/-M5kmOE6J2op2uYsHA9l)

### CORS (跨網域資源共享)

CORS = **C**ross-**O**rigin **R**esource **S**haring

解決方式，設定 **`Access-Control-Allow-Origin`** 以及 **`Access-Control-Allow-Methods`** 這兩個表頭資訊。如果有允許其它表頭資訊的話，可再設定 **`Access-Control-Allow-Headers`** 。

第一種設定方式，以 Apache 的 **`.htaccess`** 檔為例：

```bash
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods "*"

# 如果有額外帶其它表頭資訊的話，也可以設定允許的表頭資訊
Header add Access-Control-Allow-Headers "API-Key"
```

第二種設定方式，以 PHP 程式個別檔案去設定：

```php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: *");

// 如果有額外帶其它表頭資訊的話，也可以設定允許的表頭資訊
header("Access-Control-Allow-Headers: API-Key");
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.webmix.cc/ajax/2.-ji-ben-guan-nian/2.4-guan-wu-kua-wang-yu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
