views:

409

answers:

4

Hi there,

Can anyone help? I am trying to interrogate the UrlReferer whcih should contain Google.com but it contains my current site. My web page is a standard HTM page and jquery calls a static method like so

    [WebMethod]
    public static void ProcessTracking(string jsonString)

Inside this method i do a standard lookup on Request.UrlReferrer like so

    string referrerDomain = HttpContext.Current.Request.UrlReferrer.Host ;

But it always contains my current domain, this was a little suspect so i created a standard asp.net page and did the same and it works 100% without issue..

So it appears that when my htm page calls via jquery my webmethod (static) and interrogates the UrlReferrer it return "ALWAYS" my current site which is wrong.

Does anyone know a work around?

I even tried doing something in session_start etc in global.asax but no fix.

EDIT: How i am calling the page from jquery in html

     $.ajax({
        type: "POST",
        url: "MyService.aspx/ProcessTracking",
        data: jsonData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        success: function(msg) {

        },
        error: function(msg) {
            alert(error);
        }
    });
A: 

That script is hosted on your page, right? In that case, the referrer will be your site.

If you want the referrer for the page itself, then you need to pass it as an argument with your Ajax call; it won't be present in the HTTP header.

You can read the referrer of the page from the document.referrer property.

RickNZ
A: 

Surely it should contain your current domain, as that is webpage doing the post?

If you want to retrieve the original callers page, you'll need to store that in the original webpage, before calling your ajax code, and then passing that through.

Bravax
A: 

Resources called via AJAX requests will consider the calling page as the referrer, which is why your domain is showing up as the referrer.

You had the right idea to use the Global.asax, but try hooking in to the BeginRequest method:

void Application_BeginRequest(Object Sender, EventArgs e)
{
    string referrerDomain = HttpContext.Current.Request.UrlReferrer.Host ;
}
Chris Pebble
A: 

This is working as intended. When you use AJAX to post, you are sending a request from your page (your domain!) to the target server.

One workaround would be to store the original referrer's host name in a javascript variable when constructing your page:

var referrerHost = '<%= HttpContext.Current.Request.UrlReferrer.Host %>';

Then package that into the jsonData variable you're sending to the ProcessTracking method in the ajax function's data parameter.

Jeff Sternal