Plugin - AJAX GET request to cross domain

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Plugin - AJAX GET request to cross domain

Jakob Bachhuber
Hey all,

I am developing a plugin, where I wanna ask my personal web API about
some email details.
First, I tried to make a jquery ajax request.
Firebug shows me the correct response, but I always get a 404 error. So
the plugin does not get the result.
I can imagine that is because a cross domain issue.

Then I tried to do an ajax request to my server side plugin code, where
I would try to make a php request to my API. But to be honest, I can't
figure out how to send my values with such a GET request, process them
within the php function and send it back.

Of course, I have read the documentation. I also analysed some other
plugins, but it is still just random guessing for me.

Thanks for any help.

Cheers,
Jack

In my JavaScript I do:
var value = "test"
rcmail.http_get("plugin.someaction","value"+value);

In my php I do:
$this->register_action('plugin.someaction', array($this, 'actions'));

and

function actions($args)
{
       echo '<script type="text/javascript">alert("' . $args .
'")</script>';
       echo '<script type="text/javascript">alert("' . $args['value'] .
'")</script>';
       $this->rc->ouput->send();
}



_______________________________________________
Roundcube Development discussion mailing list
[hidden email]
http://lists.roundcube.net/mailman/listinfo/dev

signature.asc (484 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Plugin - AJAX GET request to cross domain

Thomas Bruederli-2
On Mon, Sep 12, 2016 at 11:21 AM, Jakob Bachhuber <[hidden email]> wrote:
> [...]
>
> Of course, I have read the documentation. I also analysed some other
> plugins, but it is still just random guessing for me.

The relevant part for you is probably here:
https://github.com/roundcube/roundcubemail/wiki/Plugin-API#ajax-requests-and-callbacks

> In my php I do:
> $this->register_action('plugin.someaction', array($this, 'actions'));
>
> and
>
> function actions($args)
> {
>        echo '<script type="text/javascript">alert("' . $args .
> '")</script>';
>        echo '<script type="text/javascript">alert("' . $args['value'] .
> '")</script>';
>        $this->rc->ouput->send();
> }

The way to pass data back to the client is not using `echo` but send
it a "command" for which your plugin has already registered an event
listener:

  $rcmail->output->command('plugin.somecallback', $args);

Makes sense?

~Thomas
_______________________________________________
Roundcube Development discussion mailing list
[hidden email]
http://lists.roundcube.net/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Plugin - AJAX GET request to cross domain

Thomas Bruederli-2
On Sun, Sep 18, 2016 at 10:53 AM, Joogl <[hidden email]> wrote:
> Thanks a lot Thomas,
> was not too hard to be honest.
>
> But I still have problems with my values I want to transfer.
> Do I really have to use $_POST? I mean, for what reason do I have the
> $args variable?

Actually, the $args variable is empty in this case.
In order to access request data, you should use the dedicated function [1]:

  rcube_utils::get_input_value('someparam', rcube_utils::INPUT_GPC);

This does proper input sanitization like html tag stripping and
charset conversion.
Use rcube_utils::INPUT_GET or rcube_utils::INPUT_POST to access GET or
POST parameters specifically.

Regards,
Thomas


[1] https://github.com/roundcube/roundcubemail/blob/master/program/lib/Roundcube/rcube_utils.php#L252


> Am 17.09.2016 um 00:56 schrieb Thomas Bruederli:
>> On Mon, Sep 12, 2016 at 11:21 AM, Jakob Bachhuber <[hidden email]> wrote:
>>> [...]
>>>
>>> Of course, I have read the documentation. I also analysed some other
>>> plugins, but it is still just random guessing for me.
>>
>> The relevant part for you is probably here:
>> https://github.com/roundcube/roundcubemail/wiki/Plugin-API#ajax-requests-and-callbacks
>>
>>> In my php I do:
>>> $this->register_action('plugin.someaction', array($this, 'actions'));
>>>
>>> and
>>>
>>> function actions($args)
>>> {
>>>        echo '<script type="text/javascript">alert("' . $args .
>>> '")</script>';
>>>        echo '<script type="text/javascript">alert("' . $args['value'] .
>>> '")</script>';
>>>        $this->rc->ouput->send();
>>> }
>>
>> The way to pass data back to the client is not using `echo` but send
>> it a "command" for which your plugin has already registered an event
>> listener:
>>
>>   $rcmail->output->command('plugin.somecallback', $args);
>>
>> Makes sense?
>>
>> ~Thomas
>>
_______________________________________________
Roundcube Development discussion mailing list
[hidden email]
http://lists.roundcube.net/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Plugin - AJAX GET request to cross domain

Thomas Bruederli-2
On Wed, Sep 21, 2016 at 3:03 AM, Joogl <[hidden email]> wrote:
> Thank you Thomas,

Please try to reply to the mailinglist and not to me privately.
Otherwise you might wait for days until you get a response :-)
>
> I very appreciate it.
> Since I have to query an external API, I am calling it with cURL.
> But whenever I do this, I get an Internal Server Error Message. But I
> can't find any logs about it. When I try it in my local workspace,
> everything works fine.
> Does Roundcube forbid cURL?

Roundcube does not forbid anything. curl needs to be installed with
PHP on your server.

Fatal errors are logged into Roundcube's error log or the error log
file of your webserver.

~Thomas


> function sendCurl($url)
> {
>     $ch = curl_init($url);
>     $curlResult["msg"] = curl_exec($ch);
>     $curlResult["http"] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
>     curl_close($ch);
>     return $curlResult;
> }
>
> Am 18.09.2016 um 23:20 schrieb Thomas Bruederli:
>> On Sun, Sep 18, 2016 at 10:53 AM, Joogl <[hidden email]> wrote:
>>> Thanks a lot Thomas,
>>> was not too hard to be honest.
>>>
>>> But I still have problems with my values I want to transfer.
>>> Do I really have to use $_POST? I mean, for what reason do I have the
>>> $args variable?
>>
>> Actually, the $args variable is empty in this case.
>> In order to access request data, you should use the dedicated function [1]:
>>
>>   rcube_utils::get_input_value('someparam', rcube_utils::INPUT_GPC);
>>
>> This does proper input sanitization like html tag stripping and
>> charset conversion.
>> Use rcube_utils::INPUT_GET or rcube_utils::INPUT_POST to access GET or
>> POST parameters specifically.
>>
>> Regards,
>> Thomas
>>
>>
>> [1] https://github.com/roundcube/roundcubemail/blob/master/program/lib/Roundcube/rcube_utils.php#L252
>>
>>
>>> Am 17.09.2016 um 00:56 schrieb Thomas Bruederli:
>>>> On Mon, Sep 12, 2016 at 11:21 AM, Jakob Bachhuber <[hidden email]> wrote:
>>>>> [...]
>>>>>
>>>>> Of course, I have read the documentation. I also analysed some other
>>>>> plugins, but it is still just random guessing for me.
>>>>
>>>> The relevant part for you is probably here:
>>>> https://github.com/roundcube/roundcubemail/wiki/Plugin-API#ajax-requests-and-callbacks
>>>>
>>>>> In my php I do:
>>>>> $this->register_action('plugin.someaction', array($this, 'actions'));
>>>>>
>>>>> and
>>>>>
>>>>> function actions($args)
>>>>> {
>>>>>        echo '<script type="text/javascript">alert("' . $args .
>>>>> '")</script>';
>>>>>        echo '<script type="text/javascript">alert("' . $args['value'] .
>>>>> '")</script>';
>>>>>        $this->rc->ouput->send();
>>>>> }
>>>>
>>>> The way to pass data back to the client is not using `echo` but send
>>>> it a "command" for which your plugin has already registered an event
>>>> listener:
>>>>
>>>>   $rcmail->output->command('plugin.somecallback', $args);
>>>>
>>>> Makes sense?
>>>>
>>>> ~Thomas
>>>>
_______________________________________________
Roundcube Development discussion mailing list
[hidden email]
http://lists.roundcube.net/mailman/listinfo/dev