ajax cross-domain post method

WBOY
Release: 2016-07-28 08:28:13
Original
1118 people have browsed it

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;
    });
Copy after login
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);
Copy after login
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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template