0

In my MVC application, I'm trying to populate two javascript variables before loading a script. But I keep getting the error that my variables aren;t set before the javascript file loads. I've been going in circles trying to figure this.

<script type="text/javascript">
        $(document).ready(function () {
            var applicationId = "@Constants.SquareUpPaymentInfo.AppID";
            var locationId = "@Constants.SquareUpPaymentInfo.LocationID";
            jQuery.getScript('https://js.squareup.com/v2/paymentform')
                .done(function () {
                    jQuery.getScript('@Url.Content("~/Content/JS/sqpaymentform.js")')
                        .fail(function () {
                            alert('Failed');
                        })
                })
                .fail(
                function () {
                        alert('There was a connection issue for the payment form library. ');
                    });
        });
</script>

The sqpaymentform.js loads but it requires applicationID and locationID to be set but it thinks that they are not set for some reason but I can easily see that they are set in the chrome developer screen.

1
  • try moving your function declarations outside of $(document).ready Commented Oct 24, 2018 at 16:32

1 Answer 1

3

You're declaring applicationId and locationId as local variables. The script needs them to be global variables, so declare them outside the function.

<script type="text/javascript">
var applicationId = "@Constants.SquareUpPaymentInfo.AppID";
var locationId = "@Constants.SquareUpPaymentInfo.LocationID";

$(document).ready(function() {
  jQuery.getScript('https://js.squareup.com/v2/paymentform')
    .done(function() {
      jQuery.getScript('@Url.Content("~/Content/JS/sqpaymentform.js")')
        .fail(function() {
          alert('Failed');
        })
    })
    .fail(
      function() {
        alert('There was a connection issue for the payment form library. ');
      });
});
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

That seems to be the iteration that I didn't try. Thanks a million. Now I just have to figure why the paymentform library is no longer doing its thing and putting designated inputs inside of iframes. It's like it's loading but not executing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.