Access the AccessBooks Web Service via an AJAX Request

Walks you through the process of sending an AJAX request to your deployed service using a browser that supports CORS.
Restriction: This topic applies only when the Enterprise Server feature is enabled.

Send an AJAX request

The service you have deployed can be accessed by client programs as well as browsers that support cross-origin resource sharing (or have an add-on installed that unblocks CORS). In this segment of the tutorial, you attempt to access the service using such a browser for the purpose of observing the results.

We provide a test script, bookAJAXtest.html, that contains an AJAX request. You run this script in a browser that supports CORS.

  1. In Windows File Explorer, browse to the %PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\Interface Mapping Toolkit\ServiceInterfaces\BookDemo directory.
  2. Open the bookAJAXtest.html file in a browser that supports cross-origin resource sharing.
  3. After the browser refreshes, click the Read radio button.
  4. Type 1111 into the lnk_b_stockno field; then click Submit.

    Nothing happens!

    In fact, this is the expected result. The reason nothing happened is that your service interface's Allowed Origin field is empty, preventing the service from recognizing that your AJAX request is originating from an allowed source. The request and response are still being sent, but your browser is preventing you from seeing the response.

  5. Close the browser window.

In the next segment, you correct this problem.

Delete the previously deployed service

You need to reconfigure the AccessBooks Web service interface to enable it to accept an AJAX request from any origin, and then redeploy it to your enterprise server instance. To prepare the ESCORS enterprise server instance to accept the redeployed service interface, you need to delete the previously deployed service.

  1. In ESCWA click Native.
  2. In the ESCWA navigation pane, expand Directory Servers > Default, and then select your ESCORS enterprise server instance.
  3. On the General drop-down list, select Services.
  4. Expand the table row for your /temppath/AccessBooks/1.0 service.
  5. Click the row for the Add operation, and then click the Delete button at the right end of the row.
  6. Repeat the same for the other operations.

Set deployment properties

  1. In the Visual Studio Solution Explorer, right-click the AccessBooks Web service; then click Properties.
  2. In the Allowed Origin field, type * (asterisk). This enables your enterprise server instance to accept all requests regardless of the origin.
  3. Save the project.

Restart the ESCORS enterprise server instance

With the previously deployed services deleted and the new deployment properties set, you need to restart the ESCORS enterprise server instance for the changes to take effect.

  • In the Visual Studio Server Explorer, right-click the ESCORS enterprise server instance, and then select Restart from the context menu.

Deploy the AccessBooks Web Service

  • From the Solution Explorer, right-click the AccessBooks service interface; then click Deploy.

The Output window shows the progression of the deployment process.

View deployment results

Now that your service interface has once again been deployed as a Web service running on the ESCORS enterprise server instance, you can look at the details of the deployed service via ESCWA.

  1. In ESCWA click Native.
  2. In the ESCWA navigation pane, expand Directory Servers > Default, and then select your ESCORS enterprise server instance.
  3. On the General drop-down list, select Services.
  4. Expand the table row for your /temppath/AccessBooks/1.0 service.

    In the rows showing your operations, notice that the Last Status column shows all four operations as Available. This means that the service is ready to be accessed by a client.

  5. Click the row for the Add operation, and then click the Edit button at the right end of the row.
  6. Notice the following in the Configuration section for this operation:
    [response headers]
    Access-Control-Allow-Credentials=false
    Access-Control-Allow-Origin=*

    The same should be in the details for the other operations. This indicates that your service is ready to be called by AJAX scripts.

Resend the AJAX request

Now that you have reconfigured and redeployed the AccessBooks Web service, try running that HTML script again.

  1. In Windows File Explorer, browse to the %PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\Interface Mapping Toolkit\ServiceInterfaces\BookDemo directory.
  2. Open the bookAJAXtest.html file in a browser that supports cross-origin resource sharing (or install an add-on that unblocks CORS).
  3. Click the Read radio button
  4. Type 1111 into the lnk_b_stockno field; then click Submit.

    The following text should appear below the Submit button:

    Result: { "lnk_file_status" : "00", "lnk_b_text_details" 
    : { "lnk_b_title" : "LORD OF THE RINGS", "lnk_b_type" 
    : "FANTASY", "lnk_b_author" : "TOLKIEN" }, 
    "lnk_b_stockno" : "1111", "lnk_b_retail" : 15, 
    "lnk_b_onhand" : 4000, "lnk_b_sold" : 3444 }
  5. Click the Next radio button, and then click Submit.

    The following text should appear below the Submit button:

    Result: { "lnk_file_status" : "00", "lnk_b_text_details" 
    : { "lnk_b_title" : "OLIVER TWIST", "lnk_b_type" 
    : "FICTION", "lnk_b_author" : "DICKENS" }, 
    "lnk_b_stockno" : "2222", "lnk_b_retail" : 10, 
    "lnk_b_onhand" : 3000, "lnk_b_sold" : 2333 }
  6. Close your browser.

This concludes the tutorial.