質問

I'm developing a REST API using Codeigniter-restserver for a mobile applications in Phonegap.

Since Phonegap loads index.html using file://, my API should support CORS. And I'm new to this CORS.

I've set headers in libraries/REST_Controller.php

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

And I'm using Backbone.js.

Here is my Controller

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

}

Here's my jQuery ajax call.

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

I'm able to access this /share/format/json API with POSTMAN, chrome extension, but not with file:// or localhost://.

EDIT:

I've also tried changing share_post() to share_gett(), It worked. But i need it in POST.

I'm stuck on this for the past 48 hours. Tried many solutions, but nothing helped me with this issue. Please help me.

役に立ちましたか?

解決

Phonegap provides option to whitelist your webservice domain. It is set up the access origin in config xml http://docs.phonegap.com/en/2.3.0/guide_whitelist_index.md.html

他のヒント

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top