我想使用JSONP调用WCF服务。有两种方法, 1.“你好”方法,它不需要任何参数。这很好用 2.“Hello1”方法采用字符串参数。这不能正常工作。它应该返回我传递的值。
以下是代码段
服务器代码
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace WcfService1
{
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string Hello()
{
return "I got the call";
}
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string Hello1(string str)
{
return "Hi, you passed me " + str;
}
}
}
网络配置代码
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="True"
automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
客户端代码
<!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>
<script src="JS/JQuery.js" type="text/javascript"></script>
<script type="text/javascript">
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
var method;
//Generic function to call WCF Service
function CallService() {
$.ajax({
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
});
}
function ServiceFailed(result) {
alert('test');
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null;
Url = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function Hello() {
var uesrid = "1";
Type = "GET";
Url = "http://localhost:52136/Service1.svc/Hello";
DataType = "jsonp"; ProcessData = false;
method = "Hello";
//debugger;
CallService();
}
function Hello1() {
debugger;
var uesrid = "1";
Type = "GET";
ContentType = "application/json; charset=utf-8";
Url = "http://localhost:52136/Service1.svc/Hello1";
DataType = "jsonp"; //ProcessData = false;
method = "Hello1";
Data = "{'str':'abc'}"; //"{'Input':'a123'}";//"{'StringValue':'1234'}";
//debugger;
CallService();
}
function ServiceSucceeded(result) {
debugger;
if (DataType == "jsonp") {
alert(result); /* Problem, While calling 'Hello1' method I do not get the value in the return string*/
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
$(document).ready(
function () {
try {
//Hello();
Hello1();
} catch (exception) { }
}
);
</script>
</head>
<body>
<form id="form1" runat="server">
<h1>
JSONP Service Client Page</h1>
</form>
</body>
</html>
在调用Hello1方法
时,有人能告诉我代码中有什么问题吗?Atul Sureka
答案 0 :(得分:3)
尝试将数据作为对象传递,而不是作为字符串传递:
function Hello1() {
debugger;
var uesrid = "1";
Type = "GET";
ContentType = "application/json; charset=utf-8";
Url = "http://localhost:52136/Service1.svc/Hello1";
DataType = "jsonp"; //ProcessData = false;
method = "Hello1";
Data = {str : 'abc'};
//debugger;
CallService();
}
答案 1 :(得分:1)
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace WcfService1
{
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string Hello()
{
return "I got the call";
}
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "SetQuoteEntities?str={str}")]
public string Hello1(string str)
{
return "Hi, you passed me " + str;
}
}
}
当我修改属性webGet时,它对我有效。