我在 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/