How To Post to Server Connect from Javascript?

It appears that dmx.parse(‘content.myserverconnect.load()’) only passes parameters over the GET method. I have 2 server connects that accept query parameters and using dmx.parse and the load() function work just fine. But I have a 3rd server connect where I do not want to pass query params, but POST the key/values instead. What is the correct JS syntax to POST to my server connect? TIA

Could you add the data to a SC Form and submit via JS instead? The Form Data component is pretty useful in this regard

The data isn’t in a form. It’s a “liked” tracker where I’m using JS to manage the state of an array of videos. If a user clicks the like button, the UI is updated and then I want the JS to POST the data using my server connect.

I mean, I could add it to a form, but that would just increase the complexity if there’s a more simple solution.

The way I handle this kind of things is:

Js example:
dmx.global.set('myvalue', data);

Then you can use {{myvalue}} anywhere

Then a hidden input on the post form
<input id="text1" name="title" type="hidden" class="form-control" dmx-bind:value="myvalue">

Thanks franse, that may be a solution I have to follow. It seems then that being able to POST to a server connect from JS has never been implemented in Wappler.

What about calling a page flow from my JS instead? Do I still need a form on the page to POST my server connect with variables?

AI is suggesting the following, but I’ve never used this dmx-serverconnect-form element

<dmx-serverconnect-form id="formUpdateLike" action="/api/utils/updateFeedLikeCount" dmx-on:success="">
    <input type="hidden" name="video_id" dmx-bind:value="">
    <input type="hidden" name="action" dmx-bind:value="">
</dmx-serverconnect-form>

The thing is seeing the Server Connect as an API.

In fact, if you use this:

And:

Then:

It's up to you what is the best way of sending those values
Server connect form, is indeed a Form that sends to server connect.
You'll need to insert those values inside a Form component

Holy moly. There’s no way to submit the dmx-serverconnect-form that I can figure out using JS. I’m going with a standard HTML form, point it to my server connect and just use JS to submit the form. At least Wappler will then submit it as POST.

The reason is the security:

I hear you talking about server connect, but why don't you use form.submit() with js code?

that’s what I’m doing now :frowning: I do wish there was a way as simple as using .load() on a server connect to POST the data rather than having to create this form, hidden elements, assign the variables=values, and form.submit() in my JS. It’s Friday, I want to stop working on this LOL

You know you can use something like

dmx.parse("myserverconnectform.submit()”)

to send the data via POST…

Yeup, that’s what I’m doing. I was just resisting having to create the form in the first place and was hoping there was an equivalent .load() function available within JS for a server connect to POST the data, like a myserverconnect.post(). I’m sure under the hood, it’s much more complicated which is why it doesn’t exist.

You can. Add the form (pretty much as easy as adding the SC component) and add a Form Data component and point it to a global such as myvalue

Use the JS to set the global value (as @franse previously suggested) then submit by JS

He wants to avoid the form.
I get where he goes and he has a point..

Basically you can't do this with a POST method:

But you can do this:

It's wrong to say that Server Connect are indeed API's ?

1 Like

I just don’t get the resistance about using the SC form. You don’t need hidden inputs, just use the form data component. The only real difference to a server connect is using the word submit instead of load. Whatever works :+1:

2 Likes

:open_mouth:

Is this possible?

Edit: Amazing, I have never seen this before

Thanks @bpj

2 Likes

Try the Form Data component

It can point to any data you want and it handles posting it. Works a treat :ok_hand:

2 Likes

dmx.parse('serverconnect1.load({name: "Ben", status: "active"})');

Ben, that uses the GET method and is where I started because I assumed including the name/value pairs would be sent over POST. This is easily testable on the server side to see if the variables are coming over $_GET or $_POST. I was crossing my fingers that there was a similar function to .load() for POST, like myserverconnect.post(). Alas, there isn’t.