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

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

高洛峰
Release: 2016-12-30 09:49:02
Original
1353 people have browsed it

The example of this article describes the method of combining jquery ajax with thinkphp's getjson to achieve cross-domain. Share it with everyone for your reference, the details are as follows:

The post in jquery should not be cross-domain. It is said on the Internet that the get can be cross-domain, but I tried it but it didn’t work, and then I made the final effort to getjson. 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])

jquery ajax结合thinkphp的getjson实现跨域的方法

Writing at php:

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

This way you can access across domains.

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 Supports access from all other domains. If it is a designated domain access, change the * number to the domain name, such as:

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

I hope this article will help everyone jQuery program Design helps.

For more related articles on jquery ajax combined with thinkphp’s getjson to achieve cross-domain methods, 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
Popular Tutorials
More>
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!