Codeigniter-restserver 不接受 POST 方法 CORS



我正在使用Codeigniter-restserverPhonegap中的移动应用程序开发一个REST API。

由于 Phonegap 使用 file:// 加载index.html,我的 API 应该支持 CORS。我是这个 CORS 的新手。

我已经在libraries/REST_Controller.php中设置了标题

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept');

我正在使用Backbone.js

这是我的控制器

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';
class Prop extends REST_Controller
{
    public function __construct() 
    {
       parent::__construct(); 
       $this->load->database();
    }
    function property_get()
    {
        ...
    }    
    function property_post()
    {
    ...
    }
    function attach_image($file_type) 
    {
            if($this->post($file_type) != ""){
                    save_base64_image($file_type,$this->post($file_type));
                    $this->email->attach($_SESSION[$file_type]);
            }
    }       
    function property_delete()
    {
        ...
    }
function share_post()
    {       
    $email_id = $this->post('emailid');
    $config['mailtype'] = "html";
    $this->email->initialize($config);
    $this->email->from('myid@gmail.com', 'mobile app');
    $this->email->to($email_id); 
    $this->email->subject('subject');
    $this->email->message('message');   
    if ( ! $this->email->send() )
    {
        $this->response("Internal server error.", 500);
    } 
    else 
    {
        $result = new stdClass();
        $result->message = 'Email has been sent.';
        $this->response($result, 200); // 200 being the HTTP response code
    }        
    }
public function send_post()
{
    var_dump($this->request->body);
}

public function send_put()
{
    var_dump($this->put('foo'));
}

}

这是我的jQuery ajax调用。

$.ajax( {
        url: PMSApp.apiUrl + "/share/format/json",
        type: 'post',
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    })
    .done(function(response) {
        console.log(JSON.stringify(response));
})
.fail(function(response) {
        console.log(JSON.stringify(response));
})
.always(function(response) {
        console.log(JSON.stringify(response));
}); 

我可以使用 POSTMAN、chrome 扩展程序访问此/share/format/json API,但不能使用 file://localhost:// .

编辑:

我也尝试将share_post()更改为share_gett(),它起作用了。但我在 POST 中需要它。

在过去的 48 小时内,我一直被困在这个问题上。尝试了许多解决方案,但没有任何帮助我解决这个问题。请帮助我。

Phonegap提供了将Web服务域列入白名单的选项。它在配置 xml 中设置访问源http://docs.phonegap.com/en/2.3.0/guide_whitelist_index.md.html

您必须使用访问控制允许原产地启动Chrome

此线程:https://superuser.com/questions/384871/overriding-access-control-allow-origin-restriction-in-google-chrome

检查此胎面:访问控制允许源

不允许源

最新更新