markzzz
markzzz

Reputation: 47945

Build a Javascript Widget : can I call an aspx?

I'd like to create my own JS widget, which it must be dinamic. I mean, for example, the html generated from the downloaded script :

<script src="www.mywebsite.it/widget/?ID=2&Category=4" type="text/javascript"></script>

must be different from :

<script src="www.mywebsite.it/widget/?ID=1&Category=5" type="text/javascript"></script>

and the Data into HTML should be taken from Database, on my server. So, I need to call an aspx page that create javascript that will create html? Or which could be the solution?

Upvotes: 0

Views: 217

Answers (2)

User.Anonymous
User.Anonymous

Reputation: 1726

The better way is to use generic handler with .ashx, if you want retrieve data from server and send data in JSON or XML. Next, the data will be inserted in page with javascript.

So, if I understand well, you do generate an .aspx that contains your template and a javascript that hold the code to navigate in Category as this if you use JQuery :

$.ajax({
  url: 'data.ashx?ID=2&Category=5',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});

Server behind (ashx) :

    private readonly JavaScriptSerializer _js = new JavaScriptSerializer();        
    public void ProcessRequest(HttpContext context)
    {         
      //Do logic and retrieve data here
      Categorys c = GetFooById(context.Request["id"]);

      context.Response.Write(_js.Serialize(c));
      context.Response.ContentType = "application/json";
    }

Upvotes: 1

Jeff Watkins
Jeff Watkins

Reputation: 6359

It seems that you'd want to use AJAX.

The script source shouldn't be dynamic (it can't be cached if it is), but the script itself could call whatever page you like to pull back data (say in JSON format) or raw markup to place in a pre-defined element.

Don't use the ASPX page to create javascript if you can help it.

Consider using a JavaScript library such as jQuery to help you.

Upvotes: 0

Related Questions