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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:58:14+00:00 2026-06-07T06:58:14+00:00

There are questions similar to this, but they involved returning objects which are automatically

  • 0

There are questions similar to this, but they involved returning objects which are automatically parsed to JSON.

I have a string that consists of JSON formatted data that I simply want to return from my WCF web service so that I can read it in ajax.

It doesn’t work by simply returning the string (I get a parser error from the ajax). I was wondering if there was a specific way that I should be returning my JSON string from the web service?

My ajax is fine, because I’ve tested it with other external json providing web services, but it doesn’t work with my own (so I’m assuming it’s the data I’m returning).

For reference, here’s the important part of the getting and returning of the JSON:

WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
return reader.ReadToEnd();

and the interface declaration:

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string DoWork();

Thank you for your time.

  • 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-06-07T06:58:16+00:00Added an answer on June 7, 2026 at 6:58 am

    If you don’t want WCF to use any formatting in your response (i.e., not to convert it to a string, which is what you currently have), you can return a Stream from the operation. That way WCF will return the bytes on the stream as they are (see example code below). You can read more about this at this post about the WCF “Raw” Programming Model.

    public class StackOverflow_11342272
    {
        [ServiceContract]
        public class Service
        {
            [OperationContract]
            [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            public Stream DoWork()
            {
                string json = "{\"name\":\"John Doe\",\"age\":33,\"married\":true}";
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
                return ms;
            }
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            WebClient c = new WebClient();
            Console.WriteLine(c.DownloadString(baseAddress + "/DoWork"));
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there have been some similar questions to this, but they haven't helped
There are lots of similar questions to this, but they all seem to involve
There are similar questions to this out there, but they are either unanswered or
I have seen similar questions to this, but they involve different types so I
There have been various similar questions, but they either referred to a too specific
I know there are questions on here similar to this, but I have not
There are questions LIKE this one, but they are not similar enough to my
I know there have been similar questions on this topic but I wasn't completely
Questions similar to this have been asked before on SO, but they're not quite
There are a lot of questions similar to this but none dealing with webforms

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.