Question

I've been on the hunt for a solution for an issue that I discovered whilst trying to deploy a project Ive been working on.

Everything works fine on the development test visual studio build but when i upload it to my iis 7 server and Ajax call that i have seems to have no session(they are null). I use the session to store my user. Ill past my code below can anyone see anything wrong with it?

    public JsonResult matchMaker(string request)
    {
        try
        {
            getCurrentUser();
        }
        catch
        {
            Response.StatusCode = 500;
            return null;
        }
        // nothing needed below cut it out as note relevant.
    }
    private void getCurrentUser()
    {
        // debug this
        HttpSessionStateBase a = Session;
        if (currentUser == null)
        {
            try
            {
                // If a noUserInSessionEx is throw it will redirect them to login gracefully - Should also log why 
                if (Session["CurrentUser"] != null)
                {
                    currentUser = (UserModel)Session["CurrentUser"];
                }
                else
                {
                    throw new noUserInSession("No user for current session");
                }
            }
            catch 
            {
                Response.Redirect("login");
            }
        }
    }

On my index page where the above method is being called via javascript i have

    public ActionResult Index()
    {
        Response.Write("Session variables: <br>") ;
        for( var i=0 ; i <Session.Contents.Count ; i++){
            Response.Write(Session.Contents[i].ToString() + "<br>");
        }
        getCurrentUser();
        Response.Write(currentUser.Email);
        return View();
    }

Javascript is as below

    function executeAJAXRequest(datain, url) {
    returnObj = false;
    if (url != "" && datain != "") {
        var request = $.ajax({
            type: "POST",
            async: false,
            cache:false,
            url: "http://localhost:51525/ajaxRequest/" + url,
            //contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "request=" + JSON.stringify(datain)
        })
        request.done(function (e) {
            //console.log("Sucessful Ajax:" + datain + " TO: " + url + "\n ResponseJSON: " + arguments[2].responseText);
            returnObj = eval(arguments[0]);
        });
        request.fail(function (textStatus) {
            console.log("Failure 2 Ajax (:()\n\n Data: " + datain
                                             + "\n TO: " + url +
                                            "\n ERROR: " + textStatus.statusText);
            returnObj = false;
        });
        return returnObj;
    }
    else {
        console.log("Failure 2 Ajax (:() No Info");
        return returnObj;
    }
}

It shows me that correct user is stored and displays the email How ever my ajax is responding with the redirect to the login page.

The session has a different id to the ajax session.

Im so lost :S any help would be mega awesomeo :)

Was it helpful?

Solution

Ok so turns out the only issue I was having was I didn't have http:// in front of my address for ajax. This cause a new session to be created. Apparently not an issue when using local host thanks for the help Sergey :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top