Html File Api Tutorial Average ratng: 9,0/10 970 votes
-->

By Rick Anderson

Provide hands-on tutorials to learn about Amazon API Gateway.

This tutorial shows how to call an ASP.NET Core web API with JavaScript, using the Fetch API.

Tech support scams are an industry-wide issue where scammers trick you into paying for unnecessary technical support services. You can help protect yourself from scammers by verifying that the contact is a Microsoft Agent or Microsoft Employee and that the phone number is an official Microsoft global customer service number. Starting in Windows 8.1 and continuing in Windows 10, Windows Update client uses Event Tracing for Windows (ETW) to generate diagnostic logs. SETUP- Installs new versions of the Windows Update client when it is available. Update ID: A GUID (indicated in the previous screen shot) that's assigned to a given update at publication time. Microsoft windows terminalservices remoteconnectionmanager admin. Event ID: 20499 Source: Microsoft-Windows-TerminalServices-RemoteConnectio. Windows Event Log Analysis Splunk App. Build a great reporting interface using Splunk, one of the leaders in the Security Information and Event Management (SIEM) field, linking the collected Windows events. Windows has a way built in to not allow these kinds of updates and I'm not 100% sure it works as advertised but this is a safe adjustment that may prevent a whoops event in the future and how you might undo them if they happen: Windows 7 has built in ways to disable driver updates from Windows Update that you can read about here. If the issue still remains, try manually reset the Windows Updates Components once again and then reinitiate the Windows Update process. Resetting Windows Update Components will fix corrupt Windows Update Components and help you to install the Windows Updates quickly. Please follow the below steps to reset the Windows Updates Components: 1.

For ASP.NET Core 2.2, see the 2.2 version of Call the web API with JavaScript.

Prerequisites

  • Complete Tutorial: Create a web API
  • Familiarity with CSS, HTML, and JavaScript

Call the web API with JavaScript

In this section, you'll add an HTML page containing forms for creating and managing to-do items. Event handlers are attached to elements on the page. The event handlers result in HTTP requests to the web API's action methods. The Fetch API's fetch function initiates each HTTP request.

The fetch function returns a Promise object, which contains an HTTP response represented as a Response object. A common pattern is to extract the JSON response body by invoking the json function on the Response object. JavaScript updates the page with the details from the web API's response.

The simplest fetch call accepts a single parameter representing the route. A second parameter, known as the init object, is optional. init is used to configure the HTTP request.

  1. Configure the app to serve static files and enable default file mapping. The following highlighted code is needed in the Configure method of Startup.cs:

  2. Create a wwwroot folder in the project root.

  3. Create a js folder inside of the wwwroot folder.

  4. Add an HTML file named index.html to the wwwroot folder. Replace the contents of index.html with the following markup:

  5. Add a JavaScript file named site.js to the wwwroot/js folder. Replace the contents of site.js with the following code:

A change to the ASP.NET Core project's launch settings may be required to test the HTML page locally:

  1. Open PropertieslaunchSettings.json.
  2. Remove the launchUrl property to force the app to open at index.html—the project's default file.

This sample calls all of the CRUD methods of the web API. Following are explanations of the web API requests.

Get a list of to-do items

In the following code, an HTTP GET request is sent to the api/TodoItems route:

When the web API returns a successful status code, the _displayItems function is invoked. Each to-do item in the array parameter accepted by _displayItems is added to a table with Edit and Delete buttons. If the web API request fails, an error is logged to the browser's console.

Add a to-do item

In the following code:

  • An item variable is declared to construct an object literal representation of the to-do item.
  • A Fetch request is configured with the following options:
    • method—specifies the POST HTTP action verb.
    • body—specifies the JSON representation of the request body. The JSON is produced by passing the object literal stored in item to the JSON.stringify function.
    • headers—specifies the Accept and Content-Type HTTP request headers. Both headers are set to application/json to specify the media type being received and sent, respectively.
  • An HTTP POST request is sent to the api/TodoItems route.

When the web API returns a successful status code, the getItems function is invoked to update the HTML table. If the web API request fails, an error is logged to the browser's console.

Update a to-do item

Updating a to-do item is similar to adding one; however, there are two significant differences:

  • The route is suffixed with the unique identifier of the item to update. For example, api/TodoItems/1.
  • The HTTP action verb is PUT, as indicated by the method option.

Delete a to-do item

To delete a to-do item, set the request's method option to DELETE and specify the item's unique identifier in the URL.

Advance to the next tutorial to learn how to generate web API help pages:

Extensions are made of different, but cohesive, components. Components can include background scripts, content scripts, an options page, UI elements and various logic files. Extension components are created with web development technologies: HTML, CSS, and JavaScript. An extension's components will depend on its functionality and may not require every option.

This tutorial will build an extension that allows the user to change the background color of any page on developer.chrome.com. It will use many core components to give an introductory demonstration of their relationships.

To start, create a new directory to hold the extension's files.

The completed extension can be downloaded here.

Create the Manifest

Extensions start with their manifest. Create a file called manifest.json and include the following code, or download the file here.

The directory holding the manifest file can be added as an extension in developer mode in its current state.

  1. Open the Extension Management page by navigating to chrome://extensions.
    • The Extension Management page can also be opened by clicking on the Chrome menu, hovering over More Tools then selecting Extensions.
  2. Enable Developer Mode by clicking the toggle switch next to Developer mode.
  3. Click the LOAD UNPACKED button and select the extension directory.

Ta-da! The extension has been successfully installed. Because no icons were included in the manifest, a generic toolbar icon will be created for the extension.

Add Instruction

Although the extension has been installed, it has no instruction. Introduce a background script by creating a file titled background.js, or downloading it here, and placing it inside the extension directory.

Background scripts, and many other important components, must be registered in the manifest. Registering a background script in the manifest tells the extension which file to reference, and how that file should behave.

The extension is now aware that it includes a non-persistent background script and will scan the registered file for important events it needs to listen for.

This extension will need information from a persistent variable as soon as its installed. Start by including a listening event for runtime.onInstalled in the background script. Inside the onInstalled listener, the extension will set a value using the storage API. This will allow multiple extension components to access that value and update it.

Most APIs, including the storage API, must be registered under the 'permissions' field in the manifest for the extension to use them.

Navigate back to the extension management page and click the Reload link. A new field, Inspect views, becomes available with a blue link, background page.

Click the link to view the background script's console log, 'The color is green.'

Introduce a User Interface

Extensions can have many forms of a user interface, but this one will use a popup. Create and add a file titled popup.html to the directory, or download it here. This extension uses a button to change the background color.

Like the background script, this file needs to be designated as a popup in the manifest under page_action.

Designation for toolbar icons is also included under page_action in the default_icons field. Download the images folder here, unzip it, and place it in the extension's directory. Update the manifest so the extension knows how to use the images.

Extensions also display images on the extension management page, the permissions warning, and favicon. These images are designated in the manifest under icons.

If the extension is reloaded at this stage, it will include a grey-scale icon, but will not contain any functionality differences. Because page_action is declared in the manifest, it is up to the extension to tell the browser when the user can interact with popup.html.

Add declared rules to the background script with the declarativeContent API within the runtime.onInstalled listener event.

The extension will need permission to access the declarativeContent API in its manifest.

The browser will now show a full-color page action icon in the browser toolbar when users navigate to a URL that contains 'developer.chrome.com'. When the icon is full-color, users can click it to view popup.html.

The last step for the popup UI is adding color to the button. Create and add a file called popup.js with the following code to the extension directory, or downloaded here.

This code grabs the button from popup.html and requests the color value from storage. It then applies the color as the background of the button. Include a script tag to popup.js in popup.html.

Reload the extension to view the green button.

Layer Logic

The extension now knows the popup should be available to users on developer.chrome.com and displays a colored button, but needs logic for further user interaction. Update popup.js to include the following code.

The updated code adds an onclick event the button, which triggers a programatically injected content script. This turns the background color of the page the same color as the button. Using programmatic injection allows for user-invoked content scripts, instead of auto inserting unwanted code into web pages.

The manifest will need the activeTab permission to allow the extension temporary access to the tabs API. This enables the extension to call tabs.executeScript.

The extension is now fully functional! Reload the extension, refresh this page, open the popup and click the button to turn it green! However, some users may want to change the background to a different color.

Give Users Options

The extension currently only allows users to change the background to green. Including an options page gives users more control over the extension's functionality, further customizing their browsing experience.

Start by creating a file in the directory called options.html and include the following code, or download it here.

Then register the options page in the manifest,

Reload the extension and click DETAILS.

Scroll down the details page and select Extension options to view the options page, although it will currently appear blank.

Last step is to add the options logic. Create a file called options.js in the extension directory with the following code, or download it here.

Four color options are provided then generated as buttons on the options page with onclick event listeners. When the user clicks a button, it updates the color value in the extension's global storage. Since all of the extension's files pull the color information from global storage no other values need to be updated.

Take the Next Step

Congratulations! The directory now holds a fully-functional, albeit simplistic, Chrome extension.

What's next?

Html File Api Tutorial
  • The Chrome Extension Overview backs up a bit, and fills in a lot of detail about the Extensions architecture in general, and some specific concepts developers will want to be familiar with.

  • Learn about the options available for debugging Extensions in the debugging tutorial.

  • Chrome Extensions have access to powerful APIs above and beyond what's available on the open web. The chrome.* APIs documentation will walk through each API.

  • The developer's guide has dozens of additional links to pieces of documentation relevant to advanced extension creation.

Recent Posts