javascript - Ajax调用restful WS总是出错(fiddler得到良好的响应)

标签 javascript jquery iis-7 wcf-rest

我在 VS 2010 中用两种简单的方法(一个 GET,另一个 POST)制作了 Restfull WS。它们看起来像:

[ServiceContract]
public interface IService1
    {

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "createUser")]
    string createUser();

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "loginUser/{email}/{password}")]
    string loginUser(string email, string password);

    }

这些方法的定义很简单:

public class Service1 : IService1
    {

 public string createUser()
        {
            return "Successful POST call !!! ";
        }

 public string loginUser(string email, string password)
        {
            return "Successful GET call !!! " + email + " - "+ password;
        }
  }

我已将此服务发布到 IIS 并在浏览器中测试了我的方法(仅 loginUser (GET) 方法,无法通过浏览器测试 createUser (POST) 方法)和方法 (loginUser ) 工作正常。 当我尝试通过 jQuery AJAX 调用方法时,我总是收到错误调用而没有任何通知。我检查了我的 fiddler ,有正确的响应。

我的Ajax方法:

$(document).ready(function(){

$("#button2").click(function(){

  $.ajax({
   type: "GET",                 
   url: "http://localhost/AutoOglasi/Service1.svc/loginUser/bole/bole",

   success: function (response) {
    alert("respons  "+response);
   },
       error: function (request, status, error) {
    alert(request.responseText+" -- " + status + " --- "+ error);
   }
     });                

});

});

我在 mozila firebug 的 XML 部分中得到了这个:

XML 解析错误:找不到元素位置:moz-nullprincipal:{ba25ef4a-f215-486e-b965-e70714c5af31} 第 1 行,第 1 列: ^

我在这里做错了什么,我只是不明白,因为 fiddler 给了我很好的回应?

最佳答案

您声明 ResponseFormat = WebMessageFormat.XmlResponseFormat ,您返回的不是 xml 而是字符串。它期望第一个字符是 < 但它是 S 这就是为什么它在位置 1 找不到元素

关于javascript - Ajax调用restful WS总是出错(fiddler得到良好的响应),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13914604/

相关文章:

javascript - 将 jQuery 转换为 Vue - 未捕获的 TypeError : n. getClientRects 不是函数

javascript - 需要有关嵌套 jquery 函数的帮助

ASP.NET 对外部链接配置文件的更改会导致 IIS 应用程序重新启动吗?

php - Windows/Temp 目录中充满了阻塞服务器的 Php Session 文件

javascript - jQuery post 大文本数据传输(最终加载)

asp.net - 使用 IIS 为项目文件夹重新映射/创建虚拟目录

javascript - 如何使用 d3 js 从多个类名中选择单个类名?

javascript - 我如何将其从 12 小时制更改为 24 小时制?

javascript - 使用push方法创建数组

jquery - 如何使 jssor slider 过渡到 FADE?