We help IT Professionals succeed at work.

Load Remote JavaScript with JQuery and Execute function

DislocatedGI asked
Last Modified: 2014-05-17
I would like to load script from a remote resource.  I am using a combination of MVC and Web API.  What I am trying to do is use the $getScript function from jquery to dynamically load a small snippet of java script.  The java script itself is being rendered by an MVC Controller action that returns a partial view.  

The reason for this is I have dynamic data that is used from the model to create the script on the fly.  

My Controller:

 public ActionResult Pricing(string id)
            using (BI.Interfaces.IWidgetService widgetService = BI.Locators.ServiceLocator.GetWidgetService(Convert.ToInt32(id)))
                BI.DTO.PricingWidget result = widgetService.GetPricingWidget(Convert.ToInt32(id));
                return PartialView("_pricing", result);

Open in new window

The Model:

 public class PricingWidget
        public string Style;
        public string Endpoint;

Open in new window

The View:

@model PODISA.BI.DTO.PricingWidget
<script type="text/javascript">
    $(document).ready(function () {
        var style = ' @Html.Raw(@Model.Style)';
        $.getJSON('@Html.Raw(Model.Endpoint)' + $('#sku').text(),
          function (data) {
              $('#pricingTable').append('<div class="HeaderRow"/>');
              $('.HeaderRow').append('<div class="HeaderCol">Min</div>');
              $('.HeaderRow').append('<div class="HeaderCol">Max</div>');
              $('.HeaderRow').append('<div class="HeaderCol">Price</div>');

              var min = 1;
              var max;
              $.each(data.Pricing, function () {
                  $('#pricingTable').append('<div class="DataRow" id=qty_' + this.Quantity + '/>');
                  $('#qty_' + this.Quantity).append('<div class="DataCol">' + min + '</div>');
                  $('#qty_' + this.Quantity).append('<div class="DataCol">' + this.Quantity + '</div>');
                  $('#qty_' + this.Quantity).append('<div class="DataCol">' + this.Price + '</div>');
                  min = this.Quantity + 1;

Open in new window

From another application - I will load the script from this view:

   <div id="pricingTable"></div>
    <div id="sku">CDBNAT1</div>

 <script type="text/javascript">
    $(document).ready(function() {

Open in new window

The idea here is the remote script is injected.  On my test page I keep getting 'Uncaught SyntaxError: Unexpected token <' - at line 1 of the remote script after it is loaded.  

This is driving me nuts.  I would appreciate any help.
Watch Question

IT Expert
Top Expert 2009
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.