Home  >  Article  >  php教程  >  jquery ajax combined with thinkphp's getjson to achieve cross-domain method

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

高洛峰
高洛峰Original
2016-12-30 09:49:021304browse

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);
    }
);

Syntax:

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

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

Writing at php:

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

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:*");

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");

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!

Statement:
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