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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:16:43+00:00 2026-05-21T14:16:43+00:00

I’m attempting to use jQuery Ajax call with Json to call a webservice which

  • 0

I’m attempting to use jQuery Ajax call with Json to call a webservice which is a pass-through and calls another webservice. This works if I don’t use ajax, works as an ajax call to the backend service directly, but does not work as ajax call to pass-through service. My question is how do I get an ajax to pass-through webservice to webservice to work?

This is the code I have so far:

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string GetData(int value);
    }

Behind webservice:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

Pass-through service:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service2 : IService1
{
    public string GetData(int value)
    {
       var service = new ServiceReference1.Service1Client("clientContractStuff");
       var testString = service.GetData(value);
       return testString;
    }
}

Page code behind:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    protected void CodeBehindWcf(object sender, EventArgs e)
    {
        var service = new ServiceReference1.Service1Client("clientContractStuff");
        var testString = service.GetData(5);
        TextBox1.Text = testString;
    }

    protected void CodeBehindWcfUsingService2(object sender, EventArgs e)
    {
        var service = new Service2();
        var testString = service.GetData(5);
        TextBox2.Text = testString;
    }
}

Javascript and Html:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
    Inherits="WcfService1.WebUserControl1" %>
<script type="text/javascript" src="Scripts/jquery-1.4.4-vsdoc.js"></script>
<script type="text/javascript" src="Scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
    function ajaxService2() {
        serviceUrl = "Service2.svc/GetData";
        $.ajax({
            type: "POST",
            url: serviceUrl,
            data: "{\"value\":\"1\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (transport) {
                var string = transport;
                $("#Text2").val(string);
            }
        });
    }

    function ajaxService1() {
        serviceUrl = "Service1.svc/GetData";
        $.ajax({
            type: "POST",
            url: serviceUrl,
            data: "{\"value\":\"2\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (transport) {
                var string = transport;
                $("#Text1").val(string);
            }
        });
    }
</script>
<p>
    <input id="Button1" type="button" value="Ajax call service 1" onclick="ajaxService1()" />
    <input id="Text1" name="Text1" type="text" /></p>
<p>
    <input id="Button2" type="button" value="Ajax call service 2" onclick="ajaxService2()" />
    <input id="Text2" name="Text2" type="text" /></p>
<p>
    <asp:Button ID="Button3" runat="server" Text="Code Behind" OnClick="CodeBehindWcf" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></p>
<p>
    <asp:Button ID="Button4" runat="server" Text="Code Behind Use Service 2" OnClick="CodeBehindWcfUsingService2" />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></p>

Everything works except Button 2 and ajaxService2 function. This throws an error: ‘The remote server returned an unexpected response: (400) Bad Request.’

  • 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-21T14:16:44+00:00Added an answer on May 21, 2026 at 2:16 pm

    In order to use Ajax, webhttpbinding must be used for the endpoint of the pass-through webservice and I assumed would also be used for the backend service.

    Looking at the autogenerated service reference code in ‘Service References\Reference.cs’:

    public interface IService1 {
    
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
        string GetData(int value);
    }
    

    It’s easy to see it’s generating code under SOAP and not REST. Since webhttpbinding is a REST binding it will not work. I changed the backend service to use basichttpbinding and everything works as it should.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.