React + Django call external API on remote server

For external API call, the remote server may contain no ‘Access-Control-Allow-Origin’ header on its requested resources. So it could be almost impossible for it to call this kind of external API in react. You can try proxy if you are aware of the domain of the remote server.

So, we should make everything included in the returned Json instead of calling thru frontend. No proxy library is needed, but normal Python Requests is requested. And I guess it is just much easier to process data and call external API in backend.

We got a lot of misleading approaches for this issue.

Wrong approach:

  1. Npm http-proxy
  2. Django http proxy

Reference:

Hi James, I got a almost same react problem as this stack overflow issue (https://stackoverflow.com/questions/43317967/handle-response-syntaxerror-unexpected-end-of-input) except the API I call in this case is external (http://www.snap21.com/api/v1/reviews?orgId=1) instead of from the django app.

According to the solutions provided by the best answer, the best way to solve Access-Control-Allow-Origin problem would be to configure the http://localhost:8080 server to send the Access-Control-Allow-Origin response header and to handle the OPTIONS request. Certainly I can do this in django settings for internal API calls, but how could I make the external server send the Access-Control-Allow-Origin response header when it is called in react app? (axios is not allowed I suppose.)

Do you have any ideas about this issue?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末而钞,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子昙篙,更是在濱河造成了極大的恐慌央勒,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,331評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異允青,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)卵沉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,372評論 3 398
  • 文/潘曉璐 我一進(jìn)店門颠锉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人史汗,你說我怎么就攤上這事琼掠。” “怎么了停撞?”我有些...
    開封第一講書人閱讀 167,755評論 0 360
  • 文/不壞的土叔 我叫張陵瓷蛙,是天一觀的道長。 經(jīng)常有香客問我戈毒,道長艰猬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,528評論 1 296
  • 正文 為了忘掉前任埋市,我火速辦了婚禮冠桃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘道宅。我一直安慰自己食听,他們只是感情好胸蛛,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,526評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著樱报,像睡著了一般葬项。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上迹蛤,一...
    開封第一講書人閱讀 52,166評論 1 308
  • 那天民珍,我揣著相機(jī)與錄音,去河邊找鬼盗飒。 笑死穷缤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的箩兽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,768評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼章喉,長吁一口氣:“原來是場噩夢啊……” “哼汗贫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起秸脱,我...
    開封第一講書人閱讀 39,664評論 0 276
  • 序言:老撾萬榮一對情侶失蹤落包,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后摊唇,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體咐蝇,經(jīng)...
    沈念sama閱讀 46,205評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,290評論 3 340
  • 正文 我和宋清朗相戀三年巷查,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了有序。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,435評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡岛请,死狀恐怖旭寿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情崇败,我是刑警寧澤盅称,帶...
    沈念sama閱讀 36,126評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站后室,受9級特大地震影響缩膝,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜岸霹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,804評論 3 333
  • 文/蒙蒙 一疾层、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贡避,春花似錦云芦、人聲如沸俯逾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,276評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽桌肴。三九已至,卻和暖如春琉历,著一層夾襖步出監(jiān)牢的瞬間坠七,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工旗笔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留彪置,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,818評論 3 376
  • 正文 我出身青樓蝇恶,卻偏偏與公主長得像拳魁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子撮弧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,442評論 2 359