如题,使用xmlhttprequest向WCF接口发送请求,不带参数的接口可以正常反回,而访问带参数的老是出错,大家看下问题出在哪?WCF服务代码:using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AjaxService
{
// Add [WebGet] attribute to use HTTP GET
[OperationContract]
[WebGet()]
public string SayHello()
{
// Add your operation implementation here
return "Hello Ajax Client";
} [OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]//使用POST方法
public string SayBye(string name )
{
// Add your operation implementation here
return string.Format("Bye Ajax Client{0}",name);
}
// Add more operations here and them with [OperationContract]
}
前台JS代码:function ajaxCall(method)
{
var xmlHttp;
try{// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest(); //实例化XMLHttpRequest对象
}
catch (e)
{
// Internet Explorer 5+
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("浏览器不支持AJAX!");
return false;
}
}
}
//绑定数据处理函数。
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
// alert(xmlHttp.responseText);
document.getElementById('txtResult').value = xmlHttp.responseText;
}
else {
alert('请求出错.');
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
}
}
}
var url = "AjaxService.svc/SayHello";
if(method=="GET")
{
xmlHttp.open("GET",url, true); //异步请求数据
xmlHttp.send(null);
}else
{
url = "AjaxService.svc/SayBye"
// Build the body of the JSON message
var body = '{"name":"sweden"}';
// Send the HTTP request
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send(body);
}
}
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AjaxService
{
// Add [WebGet] attribute to use HTTP GET
[OperationContract]
[WebGet()]
public string SayHello()
{
// Add your operation implementation here
return "Hello Ajax Client";
} [OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]//使用POST方法
public string SayBye(string name )
{
// Add your operation implementation here
return string.Format("Bye Ajax Client{0}",name);
}
// Add more operations here and them with [OperationContract]
}
前台JS代码:function ajaxCall(method)
{
var xmlHttp;
try{// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest(); //实例化XMLHttpRequest对象
}
catch (e)
{
// Internet Explorer 5+
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("浏览器不支持AJAX!");
return false;
}
}
}
//绑定数据处理函数。
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
// alert(xmlHttp.responseText);
document.getElementById('txtResult').value = xmlHttp.responseText;
}
else {
alert('请求出错.');
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
}
}
}
var url = "AjaxService.svc/SayHello";
if(method=="GET")
{
xmlHttp.open("GET",url, true); //异步请求数据
xmlHttp.send(null);
}else
{
url = "AjaxService.svc/SayBye"
// Build the body of the JSON message
var body = '{"name":"sweden"}';
// Send the HTTP request
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send(body);
}
}
=>
var body = {"name":"sweden"};
不可以啊,是不是这里要改啊xmlHttp.setRequestHeader("Content-type", "application/json");
<form style="text-align:left">
姓名: <input type="text" id="txtUsername" style="width:400px;" />
<br />
测试:<input type="button" id="btn" value="测试GET" onclick=" return ajaxCall('POST');" style="width:400px;" />
<br />
结果: <textarea id="txtResult" style="width:400px; height:200px"></textarea></form>
xmlHttp.onreadystatechange=xmlHttpChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(para); 这个顺序
并且实现javascript跨域访问WCF:参考