簡體   English   中英

相當於Python request.post()的jQuery

[英]Jquery equivalent of Python requests.post()

因此,我試圖與HackerEarth Api合作,並希望將編譯/運行功能納入我的網站。 根據可以在這里找到的文檔-https: //www.hackerearth.com/docs/wiki/developers/v3/我知道,使用以下python腳本,我可以為我的代碼獲取json響應。

   #! -*- coding: utf-8 -*-

import requests

# constants
RUN_URL = u'https://api.hackerearth.com/v3/code/run/'
CLIENT_SECRET = '5db3f1c12c59caa1002d1cb5757e72c96d969a1a'
#not my own secret key

source = "print 'Hello World'"

data = {
    'client_secret': CLIENT_SECRET,
    'async': 0,
    'source': source,
    'lang': "PYTHON",
    'time_limit': 5,
    'memory_limit': 262144,
}

r = requests.post(RUN_URL, data=data)
print r.json()

我嘗試運行上面的代碼,它工作正常。 我的網站是使用純HTML,cc,js,jquery,bootstrap構建的,而我正嘗試使用jquery post方法使此工作正常。

我試過的是-

code=document.getElementById('codeinput').value;
langaugeUsed=document.getElementById('languageSelector').value;
url='https://api.hackerearth.com/v3/code/compile/';
secret = '5db3f1c12c59caa1002d1cb5757e72c96d969a1a';    //not my own secret key


$.post(url, {client_secret: secret,
async: 0,
source: code,
lang: languageUsed,
dataType:'json',
time_limit: 5,
memory_limit: 262144 },
     function(returnedData){
        alert(returnedData);
}, 'json');

當我檢查控制台時,嘗試運行腳本后出現了以下錯誤-

無法加載資源:服務器的狀態為403(禁止訪問)index.html#:1 XMLHttpRequest無法加載https://api.hackerearth.com/v3/code/compile/ 所請求的資源上沒有“ Access-Control-Allow-Origin”標頭。 因此,不允許訪問原始“空”。 響應的HTTP狀態碼為403。

誰能幫我解決這個問題?

由於CORS,您正面臨這個問題。 默認情況下,瀏覽器不允許使用不同的URL發送請求。 因此,您需要通過添加CORS標頭來手動允許此操作。

您可以看一下CORS Python庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM