WCF服务,jQuery - IE中的错误请求,Chrome。 Firefox工作正常(跨域) [英] WCF Service, jQuery - Bad Request in IE, Chrome. Firefox working fine (Cross Domain)
问题描述
嗨朋友
从jQuery消费WCF服务时,我收到错误的请求错误。我有这么多篇文章...
Service.cs
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Runtime.Serialization;
使用 System.ServiceModel;
使用 System.Text;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Web.Script.Serialization;
使用 System.ServiceModel.Activation;
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class 服务:IService
{
public string GetCustomers( string 前缀)
{
List< object> customers = new List< object>();
customers.Add( new
{
Id = 1 ,
Name = Person1
});
customers.Add( new
{
Id = 2 ,
Name = Person2
});
return ( new JavaScriptSerializer()。Serialize(customers));
}
}
IService.cs
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Runtime.Serialization;
使用 System.ServiceModel;
使用 System.Text;
使用 System.ServiceModel.Web;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(方法= POST,ResponseFormat = WebMessageFormat.Json)]
string GetCustomers( string 前缀);
}
web.config
< ; system.serviceModel >
< 行为 >
< serviceBehaviors >
< 行为 name = ServiceBehavior >
< serviceMetadata httpGetEnabled = true / >
< serviceDebug includeExceptionDetailInFaults = true / >
< / behavior >
< / serviceBehaviors >
< ; endpointBehaviors >
< behavio r 名称 = ServiceAspNetAjaxBehavior >
< enableWebScript / >
< / behavior >
< / endpointBehaviors >
< / behavior >
< serviceHostingEnvironment aspNetCompatibilityEnabled 跨度> = true / >
< 服务 >
< service < span class =code-attribute> behaviorConfiguration = ServiceBehavior name = 服务 >
< endpoint 地址 = binding = webHttpBinding 合同 = IService behaviorConfiguration = ServiceAspNetAjaxBehavior >
< identity >
< dns value = localhost / > <! - 即使我使用IP地址进行了搜索 - >
< / identity >
< ; / endpoint >
< endpoint 地址 = mex binding = mexHttpBinding 合同 = IMetadataExchange / >
< / service >
< / services >
< / system.serviceModel >
HTMl文件
< pre lang =HTML> < !DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN http://www.w3.org/TR/html4/loose.dtd\">
< html >
< head >
< title > 演示1 < / title >
< script language = javascript 类型 = text / javascript src = jquery.min.js > < ; / script >
< script 语言 = javascript 类型 = text / javascript >
$(文档)。ready( function (){
$ .ajax({
type: POST,
contentType: 应用/ JSON; charset = utf-8,
url:' http://152.125.253.121/ MobileAppWebservices / Services / Service.svc / GetCustomers',
data:' {prefix :test}',
processData: false ,
dataType: json,
成功:功能(响应){
var customers = eval (response.d);
var html = ;
$ .each(customers, function (){
html + = < span>名称: + 此 .Name + Id: + this .Id + < / span>< br />;
});
alert(html)
},
错误: function (错误){
alert(err.status + < span class =code-string> - + err.statusText);
}
});
});
< / script >
< / head >
< body >
< /正文 >
< / html >
代码如下预计在Firefox中。但在IE浏览器中,Chrome和Opera会像400 Bad Request一样抛出异常。我在这上花了2天但没有找到任何修复。 (它的跨域托管站点位于远程,可从本地主机访问)
请帮帮我吗?
谢谢
( document )。ready( function (){
.ajax({
type: POST,
contentType: application / json; charset = utf- 8,
url:' http://152.125.253.121/MobileAppWebservices/Services/ Service.svc / GetCustomers',
data:' {prefix:test }',
processData: false ,
dataType: < span class =code-string> json,
成功: function (响应){
var customers = eval (response.d);
var html = ;
.each(customers, function (){
html + = < span>名称: + 此 .Name + Id: + this .Id + < span class =code-string> < / span>< br />;
});
alert(html)
},
错误: function (错误){
alert(err.status + < span class =code-string> - + err.statusText);
}
});
});
< / script >
< / head >
< body >
< /正文 >
< / html >
代码如下预计在Firefox中。但在IE浏览器中,Chrome和Opera会像400 Bad Request一样抛出异常。我在这上花了2天但没有找到任何修复。 (它的跨域托管站点位于远程,可从本地主机访问)
请帮帮我吗?
谢谢
Hi Friends
I am getting bad request error while consuming WCF Service from jQuery. I have following so many articles...
Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Serialization;
using System.ServiceModel.Activation;
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
public string GetCustomers(string prefix)
{
List<object> customers = new List<object>();
customers.Add(new
{
Id = 1,
Name = "Person1"
});
customers.Add(new
{
Id = 2,
Name = "Person2"
});
return (new JavaScriptSerializer().Serialize(customers));
}
}
IService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
string GetCustomers(string prefix);
}
web.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="ServiceAspNetAjaxBehavior">
<identity>
<dns value="localhost"/> <!--Even I traied with IP Address-->
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
HTMl File
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Demo 1</title>
<script language="javascript" type="text/javascript" src="jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'http://152.125.253.121/MobileAppWebservices/Services/Service.svc/GetCustomers',
data: '{"prefix": "test"}',
processData: false,
dataType: "json",
success: function (response) {
var customers = eval(response.d);
var html = "";
$.each(customers, function () {
html += "<span>Name: " + this.Name + " Id: " + this.Id + "</span><br />";
});
alert(html)
},
error: function (err) {
alert(err.status + " - " + err.statusText);
}
});
});
</script>
</head>
<body>
</body>
</html>
The code works as expected in Firefox. But in IE, Chrome and Opera throws exception like 400 Bad Request. I spent 2 days on this but not found any fix. (Its cross Domain hosted site in remote and accessed from the local host)
Please help me out?
Thanks
(document).ready(function(){
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: 'http://152.125.253.121/MobileAppWebservices/Services/Service.svc/GetCustomers', data: '{"prefix": "test"}', processData: false, dataType: "json", success: function (response) { var customers = eval(response.d); var html = "";
.each(customers, function () { html += "<span>Name: " + this.Name + " Id: " + this.Id + "</span><br />"; }); alert(html) }, error: function (err) { alert(err.status + " - " + err.statusText); } }); }); </script> </head> <body> </body> </html>
The code works as expected in Firefox. But in IE, Chrome and Opera throws exception like 400 Bad Request. I spent 2 days on this but not found any fix. (Its cross Domain hosted site in remote and accessed from the local host)
Please help me out?
Thanks
这篇关于WCF服务,jQuery - IE中的错误请求,Chrome。 Firefox工作正常(跨域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!