JS request to server not working inside if statement

So, when I place all the request code inside the if statement that checks if the user has fill out the login form, i get status = 0, no headers, no body, but whenI place the code outside the if statment , it works perfectly. Is this a bug or if this is caused by something else.

  document.getElementById("loginButton").onclick = function() {login()};
  function login() {
    if ((document.getElementById("inputPassword").value != "")&&(document.getElementById("inputID").value != "")) {

        var request = new XMLHttpRequest();
        request.open('POST', 'URL');
        request.setRequestHeader('Content-Type', 'application/json');
        request.onreadystatechange = function () {

            if (this.readyState === 4) {
             console.log('Status:', this.status);
             console.log('Headers:', this.getAllResponseHeaders());
             console.log('Body:', this.responseText);
              alert(this.responseText);
            }

        };

        var body = {
          'id': 123456,
          'password': 'password',
          'remember': true
        };

        request.send(JSON.stringify(body));

  }
 }

Hello,

Bootstrap Studio doesn't parse your JS code in any way - it just sends it directly to the browser when you are previewing your design, and writes it to disk as a js file when you export. You need to use Chrome or Firefox's Dev Tools to inspect what might be happening and if any errors are raised.

March 1, 2016 -- @Martin

https://bootstrapstudio.io/forums/topic/live-preview-bootstrap-js-component/#post-321

Hello and welcome to the forums!

JavaScript is not run in Bootstrap Studio for security purposes, so right now you need to preview in your browser. Once we develop a secure way of running JavaScript, we will enable it.

We will have time to work on this in the next releases.

2 years later (March 2016 -- March 2018) ..... and no progress.

We did work on this. After considering different approaches, we decided that there is no practical way to execute user code in the app securely.

This is why we won't add support for running JavaScript in Bootstrap Studio. I don't think it's a big problem - previewing in a real browser with full JS support is just one click away. You also get a powerful JS debugger there, which many devs can't live without.

@martin

Thanks for your response.

We did work on this. After considering different approaches, we decided that there is no practical way to execute user code in the app securely. This is why we won’t add support for running JavaScript in Bootstrap Studio.

I suppose that is one way to explain it away and simply give up on trying to accomplish it.

I don’t think it’s a big problem – previewing in a real browser with full JS support is just one click away. You also get a powerful JS debugger there, which many devs can’t live without.

In modern web development, developers often leverage Javascript which includes aspects of layout and beyond. So not having this ability and the visual disconnect and subsequent broken layout flow inside BootStrap Studio could certainly be considered very problematic during development. Given all the posts made on Tutorialzine about various JS centered frameworks you should be well aware of such uses via Javascript, so obviously for developers these are aspects they can’t live without.

Fortunately other application workflows exist that allow developers this needed aspect, as well as offering many other advantages beyond the confines and development limitations of BootStrap Studio, which can indeed cause problematic development.

@martin,

Haven't been around in a long time, but wanted to see if anything has changed with this in app? I now see Electron apps that preview JS, React, etc., so hard to imagine security remains an issue as they seem to be using webview, etc., in app. Its nice to see BSS has advanced in many other areas, but wanted to ask and see if anything has changed in this regard another 2 years later.

Thanks

I let my license expire some time ago but wanted to see if this aspect is still the case concerning JS. Anyone? ;)

JS still does not preview inside the BSS studio program. You have to preview your site in an external browser.

Ok thanks for responding @Printninja. I’ll be back in another 2 years then. ;-P

But in all seriousness, I was hoping that had perhaps changed with BSS. Like mentioned above I’ve since seen Electron based apps doing so, so I thought maybe the security concerns were no longer prevalent given those other apps, figured I would ask about BSS again. I have not updated since 4.5.8, but I see BSS continues to mature nicely. I would personally prefer if I could see in app - dynamic JS created elements, custom scripted animation with GSAP, Anime, requestAnimationFrame, Canvas, etc. Just a bit of a pity when an app created with an embedded browser needs to be previewed in another browser to see such things. ;)

I don't see the lack of in-app js as a deal-breaker. Although, I can see it being problematic if you're working on a laptop, or single screen desktop. I have three monitors, so while BSS is open on my main screen, a preview browser is open on another. Sometimes I even have the preview going on my smartphone as well via wi-fi on my network. It's actually pretty elegant.

Pinegrow is the only builder I've used that has live js in the workspace, but TBH, I've often found myself turning it off when building a site. I don't use PG much these days though.

Pinegrow is the only builder I’ve used that has live js in the workspace, but TBH, I’ve often found myself turning it off when building a site. I don’t use PG much these days though.

I would agree there too, and usually in other apps I had to turn it off due to the lag caused after every change to a page, drove me crazy. Thankfully we had the option to turn that feature off, so really for me I'm fine with opening my browser for testing.

Ok, I just wanted to inquire before considering re-upping my BSS license, good to know thanks @Printninja.