2013-07-03 151 views
1

我有一个Wcf Service,我叫它从JavaScriptJquery(AJAX)。GET 400(坏请求)与jsonp

下面是代码的一部分:

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:

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

Service1.svc:

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

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> 

从我的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'); 
    } 
}); 

} 

IE我得到的错误:Error. Parsing JSON Request failed.

Chorme不来错误的功能,但在控制台我得到一个错误:GET http://localhost... 400 (Bad Request) .

我不明白为什么它通知GET当我通过POST,以及错误的请求。

我喜欢一些帮助..

您提供
+0

因为只有GET JSONP的作品,我想。 – EnterSB

+0

@EnterSB。即使我将'POST'变成GET,我仍然会遇到这个错误。 –

+0

Web配置没有mex端点... –

回答

1

网络配置是网站,你是消费WCF服务?

如果是这样,那么“地址”标记在端点中不能为空。

如果配置是WCF服务,<service>标记添加MEX终结像

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