Help with getCallWith
-
V.VERSIONnumber
-
@Lindon Thanks for the help, so that prints 'version' rather than the value in my json on my server.
Console.print(V.VERSIONnumber);
-
@DanH The response from your server is in the
response
variable ;) -
@d-healey aahh ok, great, make sense, thanks both!
-
@d-healey said in Help with getCallWith:
@DanH The response from your server is in the
response
variable ;)@DanH so you are likely getting a string back in response, so...
reg myJSON = response.parseAsJSON(); Console.print(myJSON.VERSIONnumber);
-
@Lindon if the string is a JSON it will parse it automatically for you.
-
@Christoph-Hart said in Help with getCallWith:
@Lindon if the string is a JSON it will parse it automatically for you.
so you are saying this works?
response.VERSIONnumber
??
-
@Lindon yes
-
@Christoph-Hart cool.
-
Can you guys give me a hand with a simple POST request? I probably need a tee-shirt, but I can't see what I'm doing wrong.
In short: I'm sending my name to a php script, and I'm expecting to get a response containing a status and message formatted in json.
My php code looks like this:
https://www.mytestserver.xyz/testing/simple-response.php<?php // check if form has been submitted if (isset($_POST["name"])) { $name = $_POST["name"]; // create response array $response = array( "status" => "success", "message" => "Hello, " . $name . "!" ); // return response as JSON header("Content-Type: application/json"); echo json_encode($response); } else { // return error if form has not been submitted $response = array( "status" => "error", "message" => "No name provided." ); header("Content-Type: application/json"); echo json_encode($response); } ?>
If I navigate to the page, I get an expected response:
{ "status":"error", "message":"No name provided." }
If I use postman.co and enter in some key data, I get an expected response:
My HISE script looks like this:
HiseSnippet 928.3ocsVs0aaaCElxIbnR6BVA5dWvOo.3lXu01MzhgkYGmMikKFQoc6sBFJ5HhPQpQRkUsg7WZ+11+fsCkjsjyb6JLV8CB3b+67QdNzy0JJyXTZjm+kk4Lj2mfiKk1zIoDtDM6Hj2Wf+wYwSCOgSYRCKLtzXYYnwk4Digkf7714Gbt54uKp52e8ciIBhjxZUgPuRAgeBOiaa0N+vehKDGSRXWxy538SNbFUImnDpB.V6fGhxIzaHWyNi3bqGF48QSS3VkN1RrLCxa2wpjx3T0uIq8+UbC+JAyILBECIpV8wJQhCwNsnIobQx7ksuAAYYdKYrSMY7H7o7D9J8sjxmWYHrMht7gWu0g2NqAuQcg2vNvaCPxqCj1sFRODGS07baqEGd9X7LokoWP.ZuKTp8E06O6gmn.Oj18yH2vNVCBqhH5YCGNHD9r2KBBhY5aY58ML6Xhg8xKNIpep0lad9AGjUBsi0T6vaJ+8C5CA.GUFa3sDc33BqUIGE9sgKK00L6DUVtRBBQ8ar6BJHfKEbIKbQgjZ4JYngmkKXWv90BnDQ6E7GA9BEkHByIZ3XGPJj2.ePseeInn+yC6aSAhzjUVIG3e2KB7C7avODp3m41z4mGeYTeGt4xqOntJOVyL.nLr8ySy6OnsDCVgmHCb1UXFDtz08pqMzYFk.BTygVxpcr2JWfFy+N3ycapAUxl92QNZkHhtjYF.rmnfU0y7EQMBUk6drhK+AUo++fgcGeM0YBPDWAyOQ2G.P15.tyTV14xZhO3tfv6aZwhMZqIWBldilcC152UfQxhrqbDeKE3bDtsu9HD9sOB0cBmVSKcbTImI41yyYx21bOpgKciaMnBb0VMr8YMCa0LGhCyT93FZDUA4tK4Pub1QDKYYhfbB0ImosbWK3cD6VXIX8TrO9Hl4FqJux2lCPnMeOJ6aVUxv7CKaq+hC4lSUYPdH5xll8eu9.VboRJDD65aybqvaL.mOqsBwslPZ31xtq3+eaE26KDeHdN2RS2LF6sALBmBeHvXyCCeJd5hELpsEf6hO9W9v7J.5BUga60oDqlCm93yJxhg2FoLn5RIS3lO754tIWKOzI6XfXlLoR3ugeMFG4j8ZLNZoQTFgpUulVe+28zyCpz.XRV8pqO9Tmb3pq8X7v8GhxfWAeMk5Z+GC221bLe4VDyWsEw7jsHlmtEw7rsHludKh4admw39CHeegUkUON.JlOsZ8im2TIAtYUcKD8O0FDwOB
Server.setBaseURL("https://mytestserver.xyz/"); const var Button1 = Content.getComponent("Button1"); inline function simpleRequest() { local parameter = { "name": "thisismyname" }; Server.callWithPOST("testing/simple-response.php", parameter, function(status, response) { Console.print(trace(response)); }); }; inline function onButton1Control(component, value) { if(value) { simpleRequest(); } }; Content.getComponent("Button1").setControlCallback(onButton1Control);
The response is timing out
I'm sure it's something simple that I'm overlooking. Anyone see an issue?
-
I think you need to use the
return
keyword to get the value, and just return the array, don't use json_encode. -
@d-healey instead of echo?
Trying that, I get this error when I navigate to the page in a web browser:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
And there is no change when I try in hise
EDIT: just re-read your reply. Trying that now.
ok tried<?php // check if form has been submitted if (isset($_POST["name"])) { $name = $_POST["name"]; // create response array $response = array( "status" => "success", "message" => "Hello, " . $name . "!" ); // return response as JSON header("Content-Type: application/json"); return $response; } else { // return error if form has not been submitted $response = array( "status" => "error", "message" => "No name provided." ); header("Content-Type: application/json"); return $response; } ?>
Getting the same syntax error. I believe "return" only works within a function
-
Getting the same syntax error. I believe "return" only works within a function
Ah yeah that's right. I didn't notice it was outside a function. Try putting it in a function, see if it makes a difference...
-
@d-healey does this look right?
<?php function simpleResponse() { // check if form has been submitted if (isset($_POST["name"])) { $name = $_POST["name"]; // create response array $response = array( "status" => "success", "message" => "Hello, " . $name . "!" ); // return response as JSON header("Content-Type: application/json"); return $response; } else { // return error if form has not been submitted $response = array( "status" => "error", "message" => "No name provided." ); header("Content-Type: application/json"); return $response; } } echo $response; ?>
Not working here
-
@Dan-Korneff Where are you calling the function?
-
Actually forget about that. I just tested on my localhost and your original code works here.
I modified it slightly but I don't think it makes a difference.
<?php // check if form has been submitted $response = []; if (isset($_POST["name"])) { $name = $_POST["name"]; // create response array $response = array( "status" => "success", "message" => "Hello, " . $name . "!" ); // return response as JSON header("Content-Type: application/json"); } else { // return error if form has not been submitted $response = array( "status" => "error", "message" => "No name provided." ); header("Content-Type: application/json"); } echo json_encode($response) ?>
-
Oh I see your mistake (I think) you are missing
www.
in your url. -
@d-healey My hero!!! that was it