This article will introduce to you how to report an ajax request error and enable cross-domain requests in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Add
header("Access-Control-Allow-Origin: *");
If you want to To allow only a certain website to pass, you can set
header("Access-Control-Allow-Origin: http://test.com"); // Allow cross-domain requests initiated by test.com, Others will not pass
If it is a php framework, it needs to be placed after the namespace, not in front, otherwise an error will be reported
*.php
<?php header('Access-Control-Allow-Origin: *'); $arr = [ array('id'=>1,'title'=>'one1'), array('id'=>2,'title'=>'one2'), array('id'=>3,'title'=>'one3'), array('id'=>4,'title'=>'one4'), ]; echo json_encode($arr); ?>
index.html
<script type="text/javascript" src="jq.js"></script> <script type="text/javascript"> $.ajax({ type:'post', url: 'http://127.0.0.1/demo1/api.php', contentType: "application/x-www-form-urlencoded", dataType: 'json', success: function(res){ console.log(res) } }) </script>
Recommended learning: php video tutorial
The above is the detailed content of Ajax request error, how to enable cross-domain request in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
