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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:07:46+00:00 2026-05-14T17:07:46+00:00

After trying to format my JSON data by hand in javascript and failing miserably,

  • 0

After trying to format my JSON data by hand in javascript and failing miserably, I realized there’s probably a better way. Here’s what the code for the web service method and relevant classes looks like in C#:

[WebMethod]
public Response ValidateAddress(Request request)
{
    return new test_AddressValidation().GenerateResponse(
        test_AddressValidation.ResponseType.Ambiguous);
}

...

public class Request
{
    public Address Address;
}

public class Address
{
    public string Address1;
    public string Address2;
    public string City;
    public string State;
    public string Zip;
    public AddressClassification AddressClassification;
}

public class AddressClassification
{
    public int Code;
    public string Description;
}

The web service works great with using SOAP/XML, but I can’t seem to get a valid response using javascript and jQuery because the message I get back from the server has a problem with my hand-coded JSON.

I can’t use the jQuery getJSON function because the request requires HTTP POST, so I’m using the lower-level ajax function instead:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "http://bmccorm-xp/HBUpsAddressValidation/AddressValidation.asmx/ValidateAddress",
    data: "{\"Address\":{\"Address1\":\"123 Main Street\",\"Address2\":null,\"City\":\"New York\",\"State\":\"NY\",\"Zip\":\"10000\",\"AddressClassification\":null}}",
    dataType: "json",
    success: function(response){
        alert(response);
    }
})

The ajax function is submitting everything specified in data:, which is where my problem is. How do I build a properly formatted JSON object in javascript so I can plug it in to my ajax call like so:

data: theRequest

I’ll eventually be pulling data out of text inputs in forms, but for now hard-coded test data is fine.

How do I build a properly formatted JSON object to send to the web service?


UPDATE: It turns out that the problem with my request wasn’t the formatting of the JSON, as T.J. pointed out, but rather that my JSON text didn’t conform to requirements of the web service. Here’s a valid JSON request based on the code in the WebMethod:

'{"request":{"Address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}'

This brought up another question: When is case sensitivity important in JSON requests to ASP.NET web services (ASMX)?

  • 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-14T17:07:47+00:00Added an answer on May 14, 2026 at 5:07 pm

    The answer is very easy and based on my previous posts Can I return JSON from an .asmx Web Service if the ContentType is not JSON? and JQuery ajax call to httpget webmethod (c#) not working.

    The data should be JSON-encoded. You should separate encode every input parameter. Because you have only one parameter you should do like following:

    first construct you data as native JavaScript data like:

    var myData = {Address: {Address1:"address data 1",
                            Address2:"address data 2",
                            City: "Bonn",
                            State: "NRW",
                            Zip: "53353",
                            {Code: 123,
                             Description: "bla bla"}}};
    

    then give as a parameter of ajax request {request:$.toJSON(myData)}

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://bmccorm-xp/HBUpsAddressValidation/AddressValidation.asmx/ValidateAddress",
        data: {request:$.toJSON(myData)},
        dataType: "json",
        success: function(response){
            alert(response);
        }
    })
    

    instead of $.toJSON which come from the JSON plugin you can use another version (JSON.stringify) from http://www.json.org/

    If your WebMethod had parameters like

    public Response ValidateAddress(Request request1, Request myRequest2)
    

    the value of data parameter of the ajax call should be like

    data: {request1:$.toJSON(myData1), myRequest2:$.toJSON(myData2)}
    

    or

    data: {request1:JSON.stringify(myData1), myRequest2:JSON.stringify(myData2)}
    

    if you prefer another version of JSON encoder.

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

Sidebar

Related Questions

After trying to make a while(bool) loop and it failing because I couldn't see
After trying my hand at Perl and a little bit of C, I am
I'm trying to consume a WCF webservice using jQuery. The returned Json data is:
I'm trying to figure out what the best way to handle a JSON object
So I'm trying to document the format of the json returned by an api
Thanks for reading! I am trying to parse a JSON feed in a format
I'm trying to convert .flv videos to .ogg format. After experimenting for a while,
I am trying to extract blocks of JSON data from a data stream in
After trying this simple console input with 5, the result is shown as 53
After trying to read various articles on sending emails with attachments in PHP (I

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.