Home >Backend Development >PHP Tutorial >ajax cross-domain post method
The previous blog talked about the ajaxgetjsonp cross-domain method. Some things that need to be paid attention to are discussed below. The post method transmits data and the backend accepts it.
First go directly to the ajax code:
$(document).ready(function() { $('#submit1').click(function(){ var data = new FormData($("#form1")[0]); //$("form").serializeArray(); //formData = new FormData(data); data.append("serect", 12324234); console.log(data); $.ajax({ type:"POST", url:"http://test/fuck", data: data, crossDomain: true, contentType: false, processData: false, dataType: 'json', success:function(data) { } }); return false; });Note that you must add crossDomain: true, this line, otherwise you will report an error, oringn cross that error, and the server must be configured with cros. I use the laravel framework I'm doing the backend, so I have to change a few things.
The first is to configure the cros code in the entry file as follows:
$response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); $response->header('Access-Control-Allow-Origin', '*') ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE') ->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Origin')->send(); $kernel->terminate($request, $response);The second is to turn off token verification in kernel.php, otherwise an error will be reported at the receiving end:
AppHttpMiddlewareVerifyCsrfToken::class,
Below is the server-side php code
public function fuck(Request $request){
. I haven't touched it yet, I will make up for it later
The above introduces the ajax cross-domain post method, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.