Perfect solution to NODE. JS cross-origin problems, node. js cross-Origin

Source: Internet
Author: User

Perfect solution to NODE. JS cross-origin problems, node. js cross-Origin

My company colleagues (front-end) wrote pages over the past few days and said they could not get the desired JSON. Android iOS can get it, however, he is also a newbie and does not know why he only knows the cross-origin issue of js, and then asks me if I don't understand the frontend. I started Baidu,

Some people say that Google Chrome needs to be configured for cross-origin, and then I add -- disable-web-security next to Google Chrome's target.

However, it was found that an error was still reported and the desired data still could not be obtained. There will be no eyebrows when you keep searching.

Today, Baidu inspired me by the PHP cross-domain, So Baidu found node. js cross-origin problem, and finally I am in the app. the js routing settings contain a piece of cross-origin code to perfectly solve the problem:

Var express = require ('express '); var app = express (); // sets the cross-origin access app. all ('*', function (req, res, next) {res. header ("Access-Control-Allow-Origin", "*"); res. header ("Access-Control-Allow-Headers", "X-Requested-With"); res. header ("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS"); res. header ("X-Powered-By", '3.2.1 '); res. header ("Content-Type", "application/json; charset = UTF-8"); next () ;}); // app. listen (0, 8088 );

The above is a small part of the NODE. the perfect solution for JS cross-origin problems is to help you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.