WCF : Service call failed: 400Bad Request

Posted by Allemahesh under WCF on 7/11/2013 | Points: 10 | Views : 5346 | Status : [Member] [MVP] | Replies : 1
I am using asp.net 3.5. I have developed the WCF Service. I try to call the WCF Service form JQUERY, But I am getting the error as

Service call failed: 400 Bad Request

MY Code is as follows:-

IService:-

[ServiceContract]
public interface IService
{
[OperationContract]
//[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
CommandStatus getCommandStatus(string Id, string UnitNumber, string UnitType);
}
Service :-

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
public CommandStatus getCommandStatus(string Id, string UnitNumber, string UnitType)
{
CommandStatus objCommandStatus = new CommandStatus();
CommandsBLL objCommandsBLL = new CommandsBLL();
DataSet ds = new DataSet();
ds = objCommandsBLL.getCommandStatus(Convert.ToInt32(Id), UnitNumber, Convert.ToInt32(UnitType));

if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
objCommandStatus.ID = Convert.ToString(ds.Tables[0].Rows[0]["IOMH_ID"]);
objCommandStatus.UnitType = Convert.ToString(ds.Tables[0].Rows[0]["IOMH_UnitType"]);
}

return objCommandStatus;
}
}

Web.Config file is

<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="wsHttpBinding" contract="IService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="AjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
and Final my Jquery call is

var ID = 10;
var UnitNumber = '10001';
var UnitType = 1;

function CallService() {
var input =
{
ID: "10",
UnitNumber: "10001",
UnitType: "1"
};

//alert(JSON.stringify(input));
$.ajax({
async: true,
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "http://localhost:2878/Mtel_Profiler/Service.svc/getCommandStatus",
data: JSON.stringify(input),
success: function (userViewModel) {
var user = userViewModel;
alert(user);
},
error: function(msg) {
ServiceFailed(msg);
}
});
}

function ServiceSucceeded(msg)
{
alert('Sucess');
return false;
}

function ServiceFailed(result)
{
alert('Service call failed: ' + result.status + '' + r




Responses

Posted by: Jayakumars on: 7/11/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
refer this

http://stackoverflow.com/questions/5995425/http-400-bad-request-when-calling-wcf-service-operation
http://stackoverflow.com/questions/17569251/wcf-service-call-failed-400bad-request

Mark as Answer if its helpful to you

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response