Home> php教程> PHP开发> body text

jquery ajax combined with thinkphp's getjson to achieve cross-domain method

高洛峰
Release: 2016-12-21 16:55:09
Original
1374 people have browsed it

The example in this article describes the method of using jquery ajax combined with thinkphp's getjson to achieve cross-domain. I share it with you for your reference. The details are as follows:

Post in jquery should not be cross-domain. It is said on the Internet that get can be cross-domain, but I tried it but it didn’t work. Then I tried my best to getjson and the result was successful, haha

JS writing:

$.getJSON( "/index.php/Index/test", function(data){ alert(data.dd); } );
Copy after login

Syntax:

jQuery.getJSON(url,[data],[callback])

PHP writing:

$data['dd']='zonglonglong'; $this->ajaxReturn($data,'JSON');
Copy after login

This way it can be cross-domain Visited.

If it still doesn’t work, it prompts a problem with the same-origin policy, then on the server side, in the php file, write

header("access-control-allow-origin:*");
Copy after login

means to support access from all other domains. If it is a specified domain access, put * Change the number to a domain name, such as:

header("access-control-allow-origin:www.baidu.com");
Copy after login

I hope this article will be helpful to everyone in jQuery programming.

For more related articles on how to implement cross-domain methods using jquery ajax combined with thinkphp’s getjson, please pay attention to the PHP Chinese website!

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 Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!