0

I am trying to call a json webmethod (present in the code behind file) from browser. But get no output!

In my json.aspx.cs page: the web method is

[System.Web.Script.Services.ScriptService]
        public class _default : System.Web.Services.WebService
        {
            [WebMethod]
            [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
            public string[] UserDetails()
            {
                return new string[] { "abc", "def" };
            }
        }

When I try the following url from browser:

http://www.mydomain/json.aspx/UserDetails

I get no result! (The page is blank) I expect - the browser to show - { "abc", "def" }

Correct me if I am doing something wrong. My intention is to get the result as plain text in browser. Is it not possible to achieve if I place the webmethod in the code behind? I don't want to create a separate service for this...

3 Answers 3

1

Is _default really the name of the class of is that just a mistake in the example? Should it not be json?

    [System.Web.Script.Services.ScriptService]
    public class Json : System.Web.Services.WebService
    {
Sign up to request clarification or add additional context in comments.

Comments

0

You need a script manager on the page:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>

Then you can call your function like this:

// hook this up to a button click
function Test()
{
    PageMethods.UserDetails(Success);
}

// this is the callback from the page
function Success(Data)
{
    alert(Data);
}

Comments

0

Actually none of the above worked.

Now instead of web form-code behind, I created a new asmx page & put it in the code behind Now, it shown some config issues: To solve it, I added:

<system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>

............

in thw eb config file.

Now I am getting the output in the browser.

The only thing remaining now - is I want it as plain text (json).

But I get it as xml:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<string>abc</string>
<string>def</string>
</ArrayOfString>

I am looking to fix this also.

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.