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 913185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:30:48+00:00 2026-05-15T17:30:48+00:00

I have validated the JSON response from my C# Webmethod, so I don’t believe

  • 0

I have validated the JSON response from my C# Webmethod, so I don’t believe that’s the problem.

Am trying to parse the result using simple jQuery $.ajax, but for whatever reason I can’t get the method to correctly fire and parse the result, also incidentally can’t seem to get the function to fire the result. Are their any limits on the size of the JSON object that can be returned.

I also removed this code from inside a “Site.Master” page because it would always refresh when I hit the simple button. Do tags work correctly with jQuery elements like the button input I’m grabbing from the DOM?

function ajax() {
//var myData = { qtype: "ProductName", query: "xbox" };
var myData = { "request": { qtype: "ProductName", query: "xbox"} };
$.ajax({
    type: "POST",
    url: "/webservice/WebService.asmx/updateProductsList",
    data: {InputData:$.toJSON(myData)},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        // var msg = {__type: "Testportal.outputData", id: "li1234", message: "it's work!", myInt:101}
        alert("message=" + msg.d.ProductName + ", id=" + msg.d.Brand);
    },
    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);
        }
    }
});

}

And the page:

 <asp:Button ID="Button1" runat="server" OnClientClick="ajax();"  Text="Button" /> 

And the Serverside Webmethod:

 public class WebService : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public OutputData updateProductsList(InputData request)
    {
        OutputData result = new OutputData();
        var db = new App_Data.eCostDataContext();
        var q = from c in db.eCosts
                select c;

        if (!string.IsNullOrEmpty(request.qtype) && !string.IsNullOrEmpty(request.query))
        {
            q = q.Like(request.qtype, request.query);
        }

        //q = q.Skip((page - 1) * rp).Take(rp);
        result.products = q.ToList();

        searchObject search = new searchObject();

        foreach (App_Data.eCost product in result.products)
        {
            /* create new item list */
            searchResult elements = new searchResult()
            {
                id = product.ProductID,
                elements = GetPropertyList(product)
            };
            search.items.Add(elements);
        }
        return result;

    }

And helper classes:

    public class OutputData
{
    public string id { get; set; }
    public List<App_Data.eCost> products { get; set; }

}
public class InputData
{
    public string qtype { get; set; }
    public string query { get; set; }
}
  • 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-15T17:30:49+00:00Added an answer on May 15, 2026 at 5:30 pm

    One problem you may be having is that you aren’t doing anything to prevent the button from submitting the form and executing a full postback/reload at the same time you’re starting your $.ajax() callback.

    I’d suggest wiring this up unobtrusively instead of using the OnClientClick property, like this:

    $(document).ready(function() {
      // May need to use $('<%= Button1.ClientID %>') if your Button is 
      //  inside a naming container, such as a master page.
      $('#Button1').click(function(evt) {
        // This stops the form submission.
        evt.preventDefault();
    
        $.ajax({
          // Your $.ajax() code here.
        });
      });
    });
    

    I also agree with Oleg that you should use json2.js for your JSON stringifying and parsing. In newer browsers, that will fall back to the browsers’ native implementations of those methods, which is much faster and makes the parsing safer.

    Update:

    To answer your question about the data, no that doesn’t look quite right.

    What you want to ultimately send to the server is this (sans formatting):

    {"request":{"gtype":"ProductName","query":"xbox"}}
    

    To accomplish that, you want something like this:

    var req = { request : { qtype: "ProductName", query: "xbox" }};
    
    $.ajax({
      data: JSON.stringify(req),
      // Remaining $.ajax() parameters
    });
    

    Keep in mind that request, qtype, and query must match your server-side structure with case-sensitive accuracy.

    You can also be more verbose in defining the request object (which I prefer, personally, to keep things clear and readable):

    var req = { };
    
    req.request = { };
    
    req.request.qtype = "ProductName";
    req.request.query = "xbox";
    

    I’ve written a bit more about this here, if you’re interested: http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.