简体   繁体   English

WCF服务使用后收到错误请求

[英]getting Bad Request in post WCF service consuming

I am consuming the post wcf service in asp.net using jquery. 我正在使用jquery在asp.net中使用post wcf服务。 we can able to call the service with user login and with out login .It is worked fine with login . 我们可以通过用户登录和不登录来调用该服务。 the following image shows the post request checked in fiddler 下图显示了在提琴手中检查的发布请求 在此处输入图片说明

when i am calling same service after user login it is giving bad request the following image shows the post request checked in fiddler after user login 当我在用户登录后调用相同的服务时,它发出了错误的请求,下图显示了用户登录后在提琴手中检查的发布请求

在此处输入图片说明

In this application i have used the Identity Framework. 在此应用程序中,我使用了身份框架。

Please give me an idea to solve this one. 请给我一个解决这个问题的想法。

Jquery code: jQuery代码:

following code i have used for consuming the WCF service 我用于消费WCF服务的以下代码

 $("#btnSubmitReport").click(function () {

        // var count = document.getElementById().RatingBehavior._currentRating;
        var count = $("#<%=hdnRating.ClientID%>").val();


        if ($("input[id$='txtBannerCode']").val() == '') {
            alert('Banner Code is Required')
            $("input[id$='txtBannerCode']").focus();
        }
        else if ($('#<%= ddlIncidentType.ClientID %>').val() == "0") {
            alert('Please choose an option.')
            $('#<%= ddlIncidentType.ClientID %>').focus();
        }
        else if (count == "0") {
            alert('Driver Rating is Required.')
        }
        else if ($("textarea[id$='txtComment']").val() == '') {
            alert('Comment is Required')
            $("textarea[id$='txtComment']").focus();
        }

        else {

            ShowSendingProgress();
            var millisecondsToWait = 500;
            setTimeout(function() {

                if (checkCookie($("input[id$='txtBannerCode']").val() + $('#<%= ddlStates.ClientID %>').val()) == true) {
                    WCFJSONById();
                }
            }, millisecondsToWait);

} }); }});

function WCFJSONById() {
    var count = $("#<%=hdnRating.ClientID%>").val();        
    var objIncident2 = {};
    objIncident2.StateAbbreviation = $('#<%= ddlStates.ClientID %>').val();
    objIncident2.BannerCode = $("input[id$='txtBannerCode']").val();
    objIncident2.IncidentTypeID = $('#<%= ddlIncidentType.ClientID %>').val();
    objIncident2.DrivingSkillID = count;
    objIncident2.Comment = $("textarea[id$='txtComment']").val();
    objIncident2.Latitude = "0";
    objIncident2.Longitude = "0";
    objIncident2.DeviceID = "";
    var browser = detectBrowser();
    objIncident2.DeviceUsed = browser.toString();

    var objIncident1 = {};
    objIncident1.objIncident = objIncident2;

    //      alert(JSON.stringify(objIncident1));



    Type = "POST";
    Url = "RoadLynkService.svc/SendIncidentReport";
    Data = JSON.stringify(objIncident1);
    ContentType = "application/json; charset=utf-8; charset-uf8;";
    DataType = "json"; ProcessData = true;
    CallService();

}



function CallService() {
    $.ajax({
        async: false,
        type: Type, //GET or POST or PUT or DELETE verb
        url: Url, // Location of the service
        data: Data, //Data sent to server
        contentType: ContentType, // content type sent to server
        dataType: DataType, //Expected data format from server
        processdata: ProcessData, //True or False
        success: function (msg) {//On Successfull service call
            ServiceSucceeded(msg);
        },
        error: ServiceFailed// When Service call fails
    });
}

Wcf Service Code Wcf服务代码

 public string SendIncidentReport(IncidentInputParameters objIncident)
    {
        return IncidentManager.InsertIncident(objIncident.StateAbbreviation, objIncident.BannerCode, objIncident.IncidentTypeID,
            objIncident.DrivingSkillID, objIncident.Comment, Convert.ToDecimal(objIncident.Latitude), Convert.ToDecimal(objIncident.Longitude), objIncident.DeviceID, objIncident.DeviceUsed);
    }

In insert incident method I have written the code for inserting to database. 在插入事件方法中,我编写了用于插入数据库的代码。

Incident input parameters Class 事件输入参数类别

 public class IncidentInputParameters
{

    public string StateAbbreviation { get; set; }


    public string BannerCode { get; set; }

    public string IncidentTypeID { get; set; }


    public string DrivingSkillID { get; set; }


    public string Comment { get; set; }


    public string Latitude { get; set; }


    public string Longitude { get; set; }


    public string DeviceID { get; set; }

    public string DeviceUsed { get; set; }
}

Web.config bindings Web.config绑定

<system.serviceModel>
<behaviors>


  <endpointBehaviors>

    <behavior name="RESTEndpointBehavior">

      <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"

               automaticFormatSelectionEnabled="true"/>
    </behavior>

  </endpointBehaviors>

  <serviceBehaviors>

    <behavior name="RESTServiceBehavior">

      <serviceMetadata httpGetEnabled="true" />

      <serviceDebug includeExceptionDetailInFaults="false"/>

    </behavior>

  </serviceBehaviors>

</behaviors>

<bindings>
  <webHttpBinding>
    <binding name="RESTBindingConfiguration" crossDomainScriptAccessEnabled="true">
      <security mode="None" />
    </binding>
  </webHttpBinding>
</bindings>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
  <service behaviorConfiguration="RESTServiceBehavior" name="RoadLynkWeb.RoadLynkService">
    <endpoint address="" binding="webHttpBinding"   bindingConfiguration="RESTBindingConfiguration"
              behaviorConfiguration="RESTEndpointBehavior" contract="RoadLynkWeb.IRoadLynkService" />

    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />

  </service>
</services>
<standardEndpoints>
  <webHttpEndpoint>

    <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
  </webHttpEndpoint>
  <webScriptEndpoint>
    <standardEndpoint   crossDomainScriptAccessEnabled="true" />
  </webScriptEndpoint>
</standardEndpoints>

Service failed response 服务失败响应

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Request Error</title>
   <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
 </head>
 <body>
    <div id="content">
     <p class="heading1">Request Error</p>
     <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://localhost/RoadLynkWeb/RoadLynkService.svc/help">service help page</a> for constructing valid requests to the service. The exception message is 'Cross domain javascript callback is not supported in authenticated services.'. See server logs for more details. The exception stack trace is: </p>
  <p>   at System.ServiceModel.Dispatcher.JavascriptCallbackMessageInspector.AfterReceiveRequest(Message&amp; request, IClientChannel channel, InstanceContext instanceContext)
   at      System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.AfterReceiveRequestCore(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
</div>

thanks,purna 谢谢,普尔纳

I found the answer just we need to specify the crossDomainScriptAccessEnabled="false" instead of true 我找到了答案,只是我们需要指定crossDomainScriptAccessEnabled =“ false”而不是true

thanks , 谢谢 ,

purna 普尔纳

You have to pass an object of IncidentInputParameters . 您必须传递一个IncidentInputParameters对象。 You have made objIncident2 a property of objIncident1 which will make objIncident1 a class and objIncident2 a property of this class. 你已经做了objIncident2的属性objIncident1这将使objIncident1类和objIncident2这个类的一个属性。

So this will be translated in c# something like this IncidentInputParameters.Someproperty . 因此,这将在c#中翻译成类似IncidentInputParameters.Someproperty You simply have to pass objIncident2 as data in ajax call 您只需要在ajax调用objIncident2作为数据传递

Data = JSON.stringify(objIncident2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM