0
我有一个WCF服务,它返回一个字符串。我正在尝试使用跨域JsonP请求来调用它。这是在IE中工作,但没有其他浏览器。我在Firefox和Chrome中收到解析器错误。通过跨域JSONP AJAX呼叫通过WCF服务检索HTML字符串
从阅读各篇文章我似乎认为,可能服务需要返回结果作为一种不同的格式。任何想法都会有所帮助,这里是我的代码。
WCF服务代码
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public string SponsorLayout2(string projectScope, int projectYear, int imageHeight)
{
// Mock data
projectScope = "uk";
projectYear = 2012;
imageHeight = 42;
// Get projectId
var project = Projects.GetProjectByProjectScopeAndYear(projectScope, projectYear);
// Get project sponsor layouts
var projectSponsorLayout = ProjectSponsorLayouts.GetProjectSponsorLayout(project.Id, imageHeight);
// Return the sponsors
if (projectSponsorLayout != null)
return projectSponsorLayout.Body;
return null;
}
jQuery的AJAX调用
$.ajax({
cache: false,
type: "GET",
async: false,
data: {},
url: "http://127.0.0.1:8082/HtmlService.svc/SponsorLayout2",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
alert("success");
},
error: function (xhr) {
alert("error");
alert(xhr.statusText);
},
complete: function(jqXHR, textStatus) {
alert(textStatus);
}
});