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-21 16:55:091318browse

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

Syntax:

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

PHP writing:

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

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

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

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!

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