Ste*_*uiz 8 c# sharepoint jquery web-services asmx
我可以使用jQuery IF调用我的web服务contentType ="application/x-www-form-urlencoded; charset = utf-8"
但是,这将返回xml: <string>[myjson]</string>
如果我尝试使用"application/json; charset = utf-8"对服务进行POST,我会收到500错误,其中包含空StackTrace和ExceptionType.我的网络服务功能从未被打过,所以我不太确定如何调试这种情况.
我的方法和类使用适当的属性进行修饰,并设置为使用JSON作为其响应类型(与我的wsdl和disco文件一样).我安装了Ajax扩展和web.config中的必需条目.
这是在SharePoint服务器场上,但我不确定这会产生太大的影响.我在所有WFE上部署了web.config更改,并安装了ajax扩展.服务再次起作用,除了默认内容类型之外,它不会接受任何其他内容.
不知道我在这里缺少什么,伙计......
我的ajax电话:
$.ajax({
type: "POST",
url: "/_vti_bin/calendar.asmx/Test",
dataType: "json",
data: "{}",
contentType: "application/json; charset=UTF-8",
success: function(msg){
alert(msg);
},
error: function(xhr, msg){ alert(msg + '\n' + xhr.responseText); }
});
Run Code Online (Sandbox Code Playgroud)
我的webservice类:
[WebService(Namespace = "http://namespace")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class CalendarService : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Test()
{
return "Hello World";
}
}
Run Code Online (Sandbox Code Playgroud)
不确定它是否可以如此简单,但我正在使用 jQuery 从我的 Web 方法回调 JSON。
我看到的主要区别是类的属性
[系统.Web.脚本.服务.ScriptService]
[WebService(Namespace = "http://MyNameSpace")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Web : System.Web.Services.WebService{
[WebMethod]
public string TestMethod(){
return "Testing";
}
}
Run Code Online (Sandbox Code Playgroud)
我必须假设您使用的是 3.5 框架,因为这是公开 JSON Web 方法的唯一方法。
我的 jQuery 调用看起来几乎相同,所以没有问题。