简体   繁体   English

带有jsonp的GET 400(错误请求)

[英]GET 400 (Bad Request) with jsonp

I have a Wcf Service , I call it from JavaScript by Jquery (AJAX). 我有一个Wcf Service ,我通过Jquery (AJAX)从JavaScript调用它。

Here is part of the code: 这是代码的一部分:

IService1.cs: IService1.cs:

 [ServiceContract]
 public interface IService1
 {
   [WebInvoke(Method = "*",
   BodyStyle = WebMessageBodyStyle.Wrapped,
   RequestFormat = WebMessageFormat.Json,
   ResponseFormat = WebMessageFormat.Json)]
   [OperationContract]
   User GetUser(string userName, string password);
 }

Service1.svc.cs: Service1.svc.cs:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public User GetUser(string userName, string password)
    {
        //DO SOMETHING
    }
}

Service1.svc: Service1.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="ProjName.Service1" CodeBehind="Service1.svc.cs" %>

Web.config: Web.config文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
  <system.web>
    <compilation debug="true"/>
    <identity impersonate="false" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment  aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="ProjName.Service1">
        <endpoint address="" behaviorConfiguration="EndpBehavior" binding="webHttpBinding" contract="ProjName.IService1" />
      </service>
    </services>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name=""/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
</configuration>

The call to Wcf function from my JS page: 从我的JS页面调用Wcf函数:

function CallService() {

jQuery.ajaxSetup({
    error: function (x, e) {
        if (x.status == 0) {
            alert('You are offline!!\n Please Check Your Network.');
        } else if (x.status == 404) {
            alert('Requested URL not found.');
        } else if (x.status == 500) {
            alert('Internal Server Error.');
        } else if (e == 'parsererror') {
            alert('Error.\nParsing JSON Request failed.');
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.\n' + x.responseText);
        }
    }
});

var request = { userName: "aaa", password: "123" };
var jsondata = JSON.stringify(request);

$.ajax({
    type: "POST", //GET or POST or PUT or DELETE verb
    url: "http://localhost:xxxx/Service1.svc/GetUser", // Location of the service
    data: jsondata, //Data sent to server
    contentType: "application/json; charset=utf-8", // content type sent to server
    dataType: "jsonp", //Expected data format from server
    async: false,
    processdata: true, //True or False
    crossDomain: true, //True or False
    success: function (result) {
        alert('success');
    }
});

}

On IE I get the error: Error. Parsing JSON Request failed. IE我收到错误消息: Error. Parsing JSON Request failed. Error. Parsing JSON Request failed.

On Chorme is not coming to the function of the error, but in console I got an error: GET http://localhost... 400 (Bad Request) . Chorme并没有出现该错误的功能,但是在控制台中出现了一个错误: GET http://localhost... 400 (Bad Request) .

I do not understand why it informs an error in GET when I pass POST , and what the Bad Request. 我不明白为什么我通过POST时会通知GET错误,以及什么是错误请求。

I would love some help .. 我希望有一些帮助..

Web config you provided is for website where you are consuming wcf service ?? 您提供的Web配置适用于您正在使用wcf服务的网站?

If it is so, " address " tag can't be empty in endpoint. 如果是这样,则“ address ”标记在端点中不能为空。

If config is for WCF service, add mex endpoint in <service> tag like 如果配置用于WCF服务,请在<service>标记中添加mex端点,例如

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

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

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