Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 740233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:29:31+00:00 2026-05-14T08:29:31+00:00

I am trying to get an ajax get to a webmethod in code behind.

  • 0

I am trying to get an ajax get to a webmethod in code behind. The problem is I keep getting the error “parserror” from the jQuery onfail method.

If I change the GET to a POST everything works fine. Please see my code below.

Ajax Call

<script type="text/javascript">
        var id = "li1234";

        function AjaxGet() {
            $.ajax({
                type: "GET",
                url: "webmethods.aspx/AjaxGet",
                data: "{ 'id' : '" + id + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(msg) {
                    alert("success");

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

    </script>

Code Behind

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true,
    ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] 
public static string AjaxGet(string id)
{
    return id;
}

Web.config

        <webServices>
            <protocols>
                <add name="HttpGet"/>
            </protocols>
        </webServices>

The URL being used

……../webmethods.aspx/AjaxGet?{%20%27id%27%20:%20%27li1234%27}

As part of the response it is returning the html for the page webmethods.

Any help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:29:31+00:00Added an answer on May 14, 2026 at 8:29 am

    Before all I could say, that you choose not the easiest way. ScriptMethods is easy to use together with ASP.NET ScriptManager and not with jQuery. I’ll recommend you better use JSON-enabled WCF HTTP Services (better as RESTfull Service) instead of ASMX Webservice which you try to use now.
    Nevertheless, one can makes you code working without using any Microsoft technologies on the client side.

    First of all verify Server side.

    1. Rename webmethods.aspx to webmethods.asmx.
    2. Verify that you placed Inside of \ and a httpHandlers for asmx extension (ScriptHandlerFactory) also exist in the config:

      <configuration>
        <!-- ... -->
        <system.web>
          <webServices>
            <protocols>
              <add name="HttpGet"/>
            </protocols>
          </webServices>
          <httpHandlers>
            <!-- ... -->
            <add verb="*" path="*.asmx"
                 type="System.Web.Script.Services.ScriptHandlerFactory"
                 validate="false"/>
          </httpHandlers></system.web></configuration>
      
    3. Verify that [ScriptService] attribute ([System.Web.Script.Services.ScriptService] if you like full names) set for your class inherited from System.Web.Services.WebService.

    Now you could test the service. Open in you Web-Browser URL like http://localhost/webmethods.asmx/AjaxGet?id=li1234
    If you receive back something like
    <?xml version="1.0" encoding="utf-8" ?>
    <string xmlns="http://tempuri.org/">li1234</string>

    You can be sure that you service part works fine.

    Remark: Independ on “ResponseFormat = System.Web.Script.Services.ResponseFormat.Json” attribute the service answer with XML responses if “Content-Type:application/json;” not set in the request.

    Now we’ll fix the client code. I hope that comments which I placed in the following code explain all.

    One more small remark. In the last part of code I call one more “complex” web method:

    [WebMethod]
    [ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public OutputData AjaxGetMore (InputData input) {
        return new OutputData () {
            id = input.id,
            message = "it's work!",
            myInt = input.myInt+1
        };
    }
    

    Where

    public class OutputData {
        public string id { get; set; }
        public string message { get; set; }
        public int myInt { get; set; }
    }
    public class InputData {
        public string id { get; set; }
        public int myInt { get; set; }
    }
    

    Now only JavaScript code which use in some places JSON plugin, which could be replaced with Crockford’s json2.js, if somebody prefer it.

    var id = "li1234";
    // version 1 - works
    var idAsJson = '"' + id + '"';  // string serializes in JSON format
    $.ajax({
        type: "GET",
        url: "/webmethods.asmx/AjaxGet?id=" + idAsJson,
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            alert(msg.d);   // var msg = {d: "li1234"} 
        },
        error: function(res, status) {
            if (status ==="error") {
                // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace
                var errorMessage = $.parseJSON(res.responseText);
                alert(errorMessage.Message);
            }
        }
    });
    
    // version 2 with respect of JSON plugin
    $.ajax({
        type: "GET",
        url: "/webmethods.asmx/AjaxGet?id=" + $.toJSON(id),
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            alert(msg.d);   // var msg = {d: "li1234"} 
        },
        error: function(res, status) {
            if (status ==="error") {
                // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace
                var errorMessage = $.parseJSON(res.responseText);
                alert(errorMessage.Message);
            }
        }
    });
    // version 3 where jQuery will construct URL for us
    $.ajax({
        type: "GET",
        url: "/webmethods.asmx/AjaxGet",
        data: {id: $.toJSON(id)},
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            alert(msg.d);   // var msg = {d: "li1234"} 
        },
        error: function(res, status) {
            if (status ==="error") {
                // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace
                var errorMessage = $.parseJSON(res.responseText);
                alert(errorMessage.Message);
            }
        }
    });
    // version 4. We set "Content-Type: application/json" about our data, but we use no 
    //            not 'dataType: "json"' parameter. Then we have "Accept: */*" in the request
    //            instead of "Accept: application/json, text/javascript, */*" before.
    //            Everithing work OK like before.
    $.ajax({
        type: "GET",
        url: "/webmethods.asmx/AjaxGet",
        data: {id: $.toJSON(id)},
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            alert(msg.d);   // var msg = {d: "li1234"} 
        },
        error: function(res, status) {
            if (status ==="error") {
                // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace
                var errorMessage = $.parseJSON(res.responseText);
                alert(errorMessage.Message);
            }
        }
    });
    // version 5. If we don't place "Content-Type: application/json" in our reqest we
    //            receive back XML (!!!) response with "HTTP/1.1 200 OK" header and 
    //            "Content-Type: text/xml; charset=utf-8" which will be placed.
    //            How one can read in
    // http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx),
    //             ASP.NET AJAX will not make JSON serialized of response data for
    //             security reasons.
    $.ajax({
        type: "GET",
        url: "/webmethods.asmx/AjaxGet",
        data: {id: $.toJSON(id)},
        dataType: "json",
        //contentType: "application/json; charset=utf-8",
        success: function(msg) {
            alert(msg.d);   // var msg = {d: "li1234"} 
        },
        error: function (res, status, ex) {
            // the code here will be works because of error in parsing server response
            if (res.status !== 200) {   // if not OK
                // we receive exception in the next line, be
                var errorMessage = $.parseJSON(res.responseText);
                alert(errorMessage.Message);
            } else {
                alert("status=" + status + "\nex=" + ex + "\nres.status=" + res.status + "\nres.statusText=" + res.statusText +
                        "\nres.responseText=" + res.responseText);
            }
        }
    });
    // version 6. Send more komplex data to/from the service
    var myData = { id: "li1234", myInt: 100}
    $.ajax({
        type: "GET",
        url: "/webmethods.asmx/AjaxGetMore",
        data: {input:$.toJSON(myData)},
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            // var msg = {__type: "Testportal.OutputData", id: "li1234", message: "it's work!", myInt:101}
            alert("message=" + msg.d.message + ", id=" + msg.d.id + ", myInt=" + msg.d.myInt); 
        },
        error: function(res, status) {
            if (status ==="error") {
                // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace
                var errorMessage = $.parseJSON(res.responseText);
                alert(errorMessage.Message);
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 410k
  • Answers 410k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer array_search does the job I think May 15, 2026 at 7:30 am
  • Editorial Team
    Editorial Team added an answer You'll probably need to organize a group of elements into… May 15, 2026 at 7:30 am
  • Editorial Team
    Editorial Team added an answer The answer was to reimplement the resizeEvent and check table.horizontalScrollBar().isVisible() May 15, 2026 at 7:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.