0

I created a project in Visual Studio named 'MyProject', and Added .aspx file to it named 'MyPage.aspx'.

In 'MyPage.aspx.cs', there is a web method as shown below

        [WebMethod(EnableSession=true)]
        public static string GetDetails()
        {
            try
            {
                var data= HttpContext.Current.Session["mySession"] as myDto;
                return myDto.Username;
            }
            catch
            {
                return "Sorry";
            }
        }

Now, I created another project in that same solution named 'NewProject'. And I have a page in this project as 'NewPage.aspx', from which I am trying to call GetDetails() from 'MyPage.aspx' (MyProject).

So I tried the following code.

NewPage.aspx

 <script type="text/javascript">

        $(document).ready(function () {
            $.ajax({
                type: 'Get',
                url: 'http://localhost:2463/MyPage.aspx/GetDetails',
                success: function (data) {
                    alert(data);
                },
                error: function (response) {
                                alert('Error');
                            }
            })

        });
    </script>

but the Web Method isn't getting hit & I get the 'Error' alert displayed.

I also tried this

    $(document).ready(function () {
                    $.ajax({
                        type: "POST",
                        url: "http://localhost:2463/MyPage.aspx/GetDetails",
                        contentType: "application/json; charset=utf-8",
                        data: '{}',
                        datatype: "json",
                        success: function (msg) {
                            alert('success');
                        },
                        error: function (response) {
                            alert('Error');
                        }
                    });
    });

</script>

but no luck.

Plz Help...!!

2
  • check the details of response in the error Commented May 13, 2015 at 13:13
  • why cant you use a webservice? Commented May 13, 2015 at 13:18

3 Answers 3

1

Sounds like a CORS problem.

By default you cant access a service that is not within the origin domain (scheme, hostname, port).

Sign up to request clarification or add additional context in comments.

1 Comment

for .net 4.5 you can use cors library to activate cross-domain requests (msdn.microsoft.com/en-us/magazine/dn532203.aspx). Using json as response should help maybe too (codeproject.com/Articles/667387/…). Another probably useful link (codeproject.com/Articles/43038/…)
1

You have to make sure that link http://localhost:2463/MyPage.aspx/GetDetails is available while making jquery ajax call. For that you can run MyProject in a seperate instance of VS and then run NewProject in another instance of VS.

Comments

0

Check console in inspect element and find a solution for given error. You can call webMethod of another page. Your code seems correct. And no need to write whole URL ('http://localhost:2463/MyPage.aspx/GetDetails') of a page, Just write 'MyPage.aspx/GetDetails'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.