api interface development cross-domain considerations and settings

Because a cross-domain request will send an OPTIONS request first, it is necessary to judge the OPTIONS request and return it directly

if(strtoupper($_SERVER['REQUEST_METHOD'])== 'OPTIONS'){
      header("Access-Control-Allow-Origin:*");
      header("Access-Control-Allow-Headers:content-type");
      header("Access-Control-Request-Method:GET,POST");
      exit;
}

When returning json data, you need to add it to the header

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST');
header('Access-Control-Allow-Headers: Destination, Content-Type, User-Agent, X-Requested-With, If-Modified-Since');
header('Access-Control-Allow-Credentials: true');

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324836530&siteId=291194637