thisの例のように、共有URLにいくつかのjsonを投稿しようとしています。この例ではnodeを使用していますが、ブラウザでそれを実行しようとしています。クロスドメインポストjsonプリフライトなし
私はfetch最初でそれを試してみました:私はエラー得たことから、
fetch("https://outlook.office365.com/webhook/...",
{
method: 'POST',
headers: {
'Content-Type': 'application/json;'
},
body: JSON.stringify(this.groupCardBody()),
})
:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:1234 ' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
をしかし、私は応答を制御することはできません、と私はにmode: 'no-cors'
を追加した場合フェッチオプションを指定すると、content-type
ヘッダーが削除され、415 Unsupported Media Type
が返されます。
だから私は、単純なxhttp要求でそれを試してみましたが、それがプリフライトを行い、バック右のヘッダを取得していないとして、それはあまりにも失敗します。
let xhr = new XMLHttpRequest()
xhr.open("POST", "https://outlook.office365.com/webhook/...");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(this.groupCardBody()));
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
例で使用request
ライブラリでしたブラウザで作業するのは非常に難しく、軽量ではありません(私のwebpackedスクリプトにはほぼ2MBが追加されています)。これをラウンドする方法についてのご意見は歓迎です。私はそのオプションを持っていません。
UPDATE
受け入れ答えで示唆したように、私は、戻ってサーバーにJSONを投稿し、そこからのポストを作ることによってそれを解決し、それは以下のような単純なもので作業しました:
クライアント:
fetch("PostGroupCard?json="+
encodeURI(JSON.stringify(this.groupCardBody())),
{credentials: "same-origin"}
)
サーバー:
Function PostGroupCard(json As String)
Dim wr = WebRequest.Create("https://outlook.office365.com/webhook/...")
wr.ContentType = "application/json"
wr.Method = "POST"
Using sw = New StreamWriter(wr.GetRequestStream())
sw.Write(json)
sw.Flush()
sw.Close()
End Using
Dim r = wr.GetResponse()
Using sr = New StreamReader(r.GetResponseStream())
Dim result = sr.ReadToEnd()
End Using
End Sub
リクエストでJSONが送信される限り、クライアント側APIではプリフライトが必要です。 [プレライトされていないリクエスト](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests)は、リクエストの 'Content-Type'がURLエンコードされた複数パート形式データ、またはプレーンテキストのいずれかです。 JSONでなければならず、プリフライトリクエストがなければ、同じ発信元ポリシーが適用されないSharePointサーバー側に実際のリクエストを行う必要があります。 –
カスタムコンテンツタイプヘッダーを取り除くとうまくいくはずです... – dandavis
use jsonp http://stackoverflow.com/questions/3839966/can-anyone-explain-what-jsonp-is-in-layman-terms – Alexan