如何使用YQL向其他子域名发送POST请求?
Great question—yes, you absolutely can use YQL to send POST requests across your subdomains, and it fits perfectly with your existing setup since you can't configure CORS on your community server. Let me walk you through how to implement this properly.
How YQL Enables POST Requests
YQL supports POST calls via its execute endpoint, which lets you run sandboxed JavaScript to make HTTP requests. This acts as a middle layer between your frontend and target subdomain, bypassing browser CORS restrictions just like your current GET solution.
Step-by-Step Implementation
1. Define the POST Logic in YQL's Sandboxed Script
You'll use YQL's yahoo.yql.web.post method to construct your request. Include the target subdomain URL, required headers, and your payload. Here's a basic example:
// YQL execute script to handle POST var payload = { username: 'communityUser', content: 'New post content' }; var postRequest = yahoo.yql.web.post({ url: 'https://subdomain.yourcommunity.com/api/submit', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify(payload) }); return postRequest.responseText;
2. Wrap the Script in a YQL Query & Call It From Your Frontend
To execute this script from your community's code, you'll wrap it in a YQL select * from execute query, then load it via JSONP (to avoid any remaining CORS issues). Here's a reusable function you can use:
// Function to send YQL POST requests function sendCrossSubdomainPost(targetUrl, postPayload) { // Encode the YQL script to safely pass it in the query const yqlScript = ` var payload = ${JSON.stringify(postPayload)}; var request = yahoo.yql.web.post({ url: "${targetUrl}", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }); return request.responseText; `; // Build the full YQL query URL const yqlQuery = `select * from execute where script='${encodeURIComponent(yqlScript)}'`; const queryUrl = `https://query.yahooapis.com/v1/public/yql?q=${encodeURIComponent(yqlQuery)}&format=json&callback=handleYQLPostResponse`; // Inject a script tag to run the query (JSONP method) const scriptTag = document.createElement('script'); scriptTag.src = queryUrl; document.body.appendChild(scriptTag); } // Callback function to process the response function handleYQLPostResponse(response) { if (response.query.results) { console.log('POST Request Successful:', response.query.results); // Add your response handling logic here (e.g., update UI) } else { console.error('POST Request Failed:', response.query.error?.description || 'Unknown error'); } }
3. Critical Tips for Your Community Setup
- API Compatibility: Confirm your target subdomain's API accepts requests from YQL's server IPs (most public APIs do, but check if your community backend has restrictions).
- Payload Adjustments: If your API uses
application/x-www-form-urlencodedinstead of JSON, format thebodyas a URL-encoded string (e.g.,key1=value1&key2=value2) and update theContent-Typeheader accordingly. - Script Approval: Since your existing YQL GET setup is already approved by your community provider, this POST implementation should pass review—just ensure you avoid any unsafe operations (YQL's sandbox blocks most risky actions by default).
内容的提问来源于stack exchange,提问作者shaedrich

