Make an AJAX Get Request with ASP/VB.NET

Posted on 2012-09-17
Last Modified: 2012-09-18

I have an 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.

Question by:sglew
    LVL 19

    Expert Comment

    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.
    LVL 11

    Accepted Solution

    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" "">
    <html xmlns="">
    <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

    LVL 19

    Expert Comment

    But this will make an asynchronous call to the server, not an AJAX call (although it could sound the same, they aren't)
    LVL 11

    Expert Comment

    @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.

    Author Closing Comment

    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!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now