These are chat archives for opal/opal

16th
Aug 2017
Bernhard Weichel
@bwl21
Aug 16 2017 08:06

Hi I have problems to do an ajax-request:

  def refresh_preview_callbacktext = @editor.get_abc_part
    result = analyze_course_description(text)
    HTTP.post("vrmd2html.php", payload:{md_text: result[:de]}).then do |response|
      `debugger`
      alert(Native(response)[:body])
    end
  end

It invokes the ajax handler (vrmd2html.php) but I have no $_POST.

Bernhard Weichel
@bwl21
Aug 16 2017 08:49
In other words: How can I access the payload in PHP?
Bernhard Weichel
@bwl21
Aug 16 2017 09:05
I think the documentation should express more about the options ... I found the solution: use data instead of payload
    HTTP.post("vrmd2html.php", data: {md_text: result[:de]}).then do |response|
      alert(Native(response)[:body])
    end
Ilya Bylich
@iliabylich
Aug 16 2017 10:04
@bwl21 I think it should be HTTP.post('/path', payload).then ...
Bernhard Weichel
@bwl21
Aug 16 2017 10:41

@iliabylich the problem: with payload, I could retrieve the raw payload in php and parse it.

$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

Now I have the working solution:

    HTTP.post("vrmd2html.php", data: {md_text: result[:de]}).then do |response|
      Element.find("#preview_de").html = JSON.parse(response.body)[:result]
    end

with php:

$input = $_POST['md_text'];
$m = new VrMarkdown();
$result = $m->transform($input);
echo(json_encode(array('result' => $result)));