Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gặp vấn đề với request module #3

Open
hkb94694 opened this issue May 1, 2016 · 6 comments
Open

Gặp vấn đề với request module #3

hkb94694 opened this issue May 1, 2016 · 6 comments

Comments

@hkb94694
Copy link

hkb94694 commented May 1, 2016

Hi anh Tùng,

Em setup source của anh dùng thư viện fetch để tương tác với API thì okie, nhưng khi sử dụng với module request thì gặp vấn đề vể CORS (anh xem hình giúp em), anh có gặp qua trường này chưa? Giải quyết như thế nào anh?

Vì mục đích của em muốn sử dụng cần đến thư việc request để có thể sử dụng cookie file và user-agent

image

@thanhtungdp
Copy link
Owner

Cái này mình làm vẫn bình thường
screen shot 2016-05-01 at 10 33 10 pm
upload code của bạn lên mình xem thử

@thanhtungdp
Copy link
Owner

screen shot 2016-05-01 at 10 35 03 pm

@hkb94694
Copy link
Author

hkb94694 commented May 1, 2016

Em không sử dụng module whatwg-fetch mà dùng module request để tương tác API
https://www.npmjs.com/package/request

Code em đơn giản chỉ có vậy thôi, nhưng em gặp phải lỗi Cross domain.
monosnap 2016-05-01 22-44-17

@thanhtungdp
Copy link
Owner

Sao không dùng axios, hay fetch bạn. Module request chỉ dành cho server thôi bạn.
Nó không dành cho frontend bình thường

@hkb94694
Copy link
Author

hkb94694 commented May 1, 2016

Chắc em đang bị hiểu lầm giữa 2 cái này em tưởng request dùng được cho cả frontend luôn. Vì nhu cầu của em cần tìm 1 thư viện tương tự curl của PHP để có thể setHeader, setCookie trong request khi gửi đi luôn. Vì có những server API không set cho mình cái Access-Control-Allow-Origin trong header nên nhiều khi bị lỗi.

Ví dụ API như thế này nè anh, trên browser vẫn ra kết quả, nhưng gọi bằng axios hay fetch thì đều báo lỗi CORS domain

http://www.cooky.vn/directory/search?q=&st=2&pageSize=40

@thanhtungdp
Copy link
Owner

Chắc bạn không tìm hiểu kỹ về vấn đè này chăng. Các hệ thống đều tương tác qua API, mà trình duyêt là nơi tương tác API, nên việc gửi header là chuyện đơn giản thôi.
https://developers.google.com/web/updates/2015/03/introduction-to-fetch?hl=en
https://davidwalsh.name/fetch
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
Tuy nhiên với vấn đề CORS domain thì giải quyết không được trên trình duyệt, Và nó chỉ làm việc tốt trên server
https://jakearchibald.com/2015/thats-so-fetch/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants