[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

Make an AJAX Get Request with ASP/VB.NET


I have an asp.net site that uses VB for code-behind.  I'm trying to integrate a login for another site into the application and the vendor requires I make an AJAX GET request to them to make the login happen and then forward the user on success to their home page. Is there an easy way to do this with .Net?  I thought maybe an httpwebrequest would do it, but was unsuccessful in doing so.

I'll attach the example HTML file the vendor sent us (login.html with their domain removed for privacy), of the call that they have that seems to work.  I've tried implementing this into my aspx page, also attached (testWS.aspx), but every single time I try that code I receive the error message.

I would prefer to handle this somehow in the VB code-behind rather than Javascript etc. on the aspx page, but will do that if I have to.  Essentially I would like to know if this can be done with the code-behind and if so how can I accomplish that, if that isn't an option can someone let me know what may be the problem with my javascript GET request on the aspx page?

Any help or guidance would be greatly appreciated.

  • 2
  • 2
1 Solution
AJAX stands for Asynchronous Javascript And XML, and is a call made from client code to a server.

I'm not sure if there's any way to make an AJAX call from VB code behind, but I will bet there's no way to do it.
Kelvin McDanielSr. Developer/ConsultantCommented:
Assuming that you're using v4.0 of the .NET Framework, this becomes easy. Essentially all that's different from your normal work is to add the Async="true" attribute and value to the @Page directive. It looks something similar to this...

The .ASPX page:
<%@ Page Language="VB" Async="true" AutoEventWireup="false" CodeFile="testWS.aspx.vb" Inherits="testWS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:button runat="server" ID="btn1" Text="Try this" />

Open in new window

The Code-behind page:
Imports System.Net

Partial Class testWS
	Inherits System.Web.UI.Page

	Public Event DownloadStringCompleted As DownloadStringCompletedEventHandler

	Protected Sub btn1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

		Dim wc As New WebClient()
		AddHandler wc.DownloadStringCompleted, AddressOf DownloadStringCallback

		Dim uri As Uri = New Uri("'http://vendorsdomain/services/userLogin/?userId=1235&typeId=1", UriKind.Absolute)

	End Sub

	Private Sub DownloadStringCallback(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)

		If Not String.IsNullOrWhiteSpace(e.Result) AndAlso e.Result.ToLowerInvariant().Contains("{whatever_indicates_success}") Then

			' Do your server flip here.
			Response.Redirect("http://vendorsdomain/index.jsp", True)
		End If

	End Sub
End Class

Open in new window

But this will make an asynchronous call to the server, not an AJAX call (although it could sound the same, they aren't)
Kelvin McDanielSr. Developer/ConsultantCommented:
@Bardobrave That is correct. I chose not to get into it, but a GET call, AJAX or not, is simply a read-only server request. The receiving server could care less if it's a true AJAX request or not... it processes them all the same. My example should have the same net effect.

There actually is a way to do this truly AJAX, but it's a kludge and more work than work I've provided if not absolutely critically necessary.
sglewAuthor Commented:
The vendor ran into some additional issues and decided to go a different route, however I do believe this would have worked for us.

I'll keep it for future reference!


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now