通过跨域 JSONP AJAX 调用通过 WCF 服务检索 HTML 字符串

发布于 2025-01-05 16:21:47 字数 1340 浏览 0 评论 0原文

我有一个返回字符串的 WCF 服务。我试图使用跨域 JsonP 请求来调用它。这在 IE 中有效,但在其他浏览器中无效。我在 Firefox 和 Chrome 中收到解析器错误。

通过阅读各种文章,我似乎认为该服务可能需要以不同的格式返回结果。任何想法都会有帮助,这是我的代码。

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); 
      } 
});

I have a WCF Service which returns a string. I am trying to call it using a cross domain JsonP request. This is working in IE but no other browser. I get a parser error back in Firefox and Chrome.

From reading through various articles i seem to think that maybe the service needs to be returning the result back as a different format. Any ideas would be helpful, here is my code.

WCF Service Code

[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 Call

$.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); 
      } 
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2025-01-12 16:21:47

我发现了为什么我会遇到上述问题,并想与您分享。
之间存在冲突,

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

由于某种原因,位于我的类顶部的

public class MyClass

这个属性和我的 web.config 文件中的这条规则

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

我最终在我的 web.config 中注释了该规则,一切都变得栩栩如生。因为我的服务是 AJAX 就绪服务,所以该属性被添加到开箱即用的类之上。无论如何,这对我有用,并希望它能帮助处于相同情况的其他人。

I found out why I was getting the problem stated above and thought I would share this with you.
For some reason there was a conflict between this attribute

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

which sits on top of my class

public class MyClass

and this rule in my web.config file

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

I have ended up commenting the rule out in my web.config and everything come to life. Because my service is an AJAX ready service, the attribute is added above the class out of the box. Anyway this worked for me and hope it helps anyone else in the same situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文