skip to Main Content

I have A web API function that returns Facebook Id for the current user, I get the Id from session. The API function displays the id correctly but when I connect to it through a JQuery function it reruns empty string and when I try the Web API function in my browser I can see the result correctly but when I open the source page I can see only empty string Like this:
enter image description here

enter image description here

This is my function code:

public class successController : ApiController
{
    [HttpGet]
    [Route("api/success")]
    public string success()
    {
        string val = "";
        if (System.Web.HttpContext.Current.Session["user"] != null)
        {
            val = System.Web.HttpContext.Current.Session["user"].ToString();
            return val;
        }
        else
            return "";
    }
}

2

Answers


  1. are you sure jquery call use get method or post?.

    you can install a web sniffer like fiddler to check what differences between each call.

    Login or Signup to reply.
  2. For the same code and logic it is working for me. Instead of cheking user session, I am returning a hard-coded string.

            public class SuccessController : ApiController {
        [HttpGet]
        [Route("api/success")]
        public string success() {
            return "2321312312312";
        }
    }
    

    }

    Below is my Jquery Code

     <script>
        $(function () {
            $.get("http://localhost:2000/api/success", function (response) {
                console.log(response); // server response
            });
        });
    
    </script>
    

    Screenshot

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search