There might be situations, where we need to navigate to a new View / Page which does not be part of _layout.cshtml (that is the place where the common includes go). It might help there to check if jQuery is loaded and if not, then it can be included on that page on demand.
An example that worked for me is,
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></' + 'script>');
}
</script>
I got this reference from a website (which I don't remember), but there are many interesting flavors to it available on the web....
Happy coding....
Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts
Wednesday, April 2, 2014
Wednesday, March 12, 2014
HTML 5 tutorial - How to access web service (Web API) from HTML file
In this tutorial, we will go over, how to retrieve data using ajax and display it within our HTML page.
To start with, will create a new ASP.NET empty web application and keep adding files as and when we need to do.
Select New project option from the menu and select the following option :
Once you do that, the solution explorer should be loaded with your empty file structure.
Now lets add a HTML file and call it HTML_API. The most important task for me to achieve in this HTML will be to invoke a web service using ajax call and display the results within a table structure.
- You could either download the file here and include it in your project
- You could use any of the CDN's below and refer them online.
CDN's are nothing but Content Delivery Network. The purpose of CDN is, they host the required files and by referring them, you can still achieve the same output.
Below is a sample of various CDN options (excludes including through physical path).
In our tutorial, I will be using google api's to refer for jQuery. More over, I will be using the same Web API tutorial (which I used for MVC Grid) to display data.
Below is the ajax code to invoke Web Service from HTML 5 page.
What is being done here is, invoking the web service and on its successful return with data, we are creating a table row and inserting the values within <td> tags. The whole content is finally appended to the table with id "category" and that would complete the getting results displayed within the table tag.
Below is the image for having the table tag as defined above.
Earlier while testing, I had this error occurring and unable to get the data from database. That's when I realized that I needed the following line.
jQuery.support.cors = true;
CORS are nothing but, Cross Origin Resource Sharing and mainly used for Cross-domain requests.
The final result is below:
Happy coding...
Subscribe to:
Posts (Atom)






