简体   繁体   中英

ajax crossdomain post to php kohana 3.2 not working

I have 2 (local) subdomains: kohana.local.com and wordpress.local.com . jQuery plugin (.js) is located on kohana.local.com domain and takes care of rating articles and retrieving rate count on both domains. Controller_Rating extends Controller. Method (action_getrating) has following code (kohana 3.2):

if($this->request->post() && $this->request->is_ajax()){
    $this->auto_render = FALSE;
    echo "{$_REQUEST['callback']}(".json_encode($data).")";
}

Ajax call:

$.ajax({
    type: "POST",
    url: "http://kohana.local.com/rating/getrating",
    dataType: "jsonp",
    data: { some_id: id },
    success: function(json){
            //do something
    }
});

When ajax call gets issued from kohana.local.com, everything works great. If it's issued from wordpress.local.com $this->request->is_ajax() is false, and method is not "post", but "get" somehow. What is the reason for this, and how to make it work? Post is required and is_ajax is good for security and validation.

Thanks in advance.

EDIT: post to jsonp is not possible, so i can't use this approach. i'll have to try to find the solution in the direction of json

You can simply use json dataType, so you don't need to use callbacks. Just add header Access-Control-Allow-Origin to server which requests are made (kohana.local.com).

All domains are allowed:

Access-Control-Allow-Origin: *

Or specify allowed domain:

Access-Control-Allow-Origin: http://wordpress.local.host

Spec: http://www.w3.org/TR/2008/WD-access-control-20080912/#access-control-allow-origin


Multiple domain solution: Access-Control-Allow-Origin Multiple Origin Domains?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM