Ajax调用宁静的WS总是出错(提琴手得到了很好的响应) [英] Ajax call restful WS is always getting error (fiddler gets the good response)
问题描述
我用两种简单的方法(一种GET,另一种POST)制作了VS 2010中的Restfull WS.它们看起来像:
I have made Restfull WS in VS 2010 with two simple method (one GET, another POST). They look like:
[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);
}
这些方法的定义很简单:
Definition of these methods is simple:
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调用方法时,总是收到错误通知,而没有任何通知.我检查了提琴手,并得到了正确的答复.
I have published this service to IIS and tested my method in browser (only loginUser (GET) method, cannot test createUser (POST) method by the browser) and method (loginUser ) work fine. When I tried to call method by jQuery AJAX, I am always getting error call without any notification. I checked my fiddler and there are the right response.
我的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);
}
});
});
});
我在XML的mozila萤火虫部分中得到了这个信息:
I mozila firebug i section XML I get this:
XML解析错误:找不到元素位置:moz-nullprincipal:{ba25ef4a-f215-486e-b965-e70714c5af31}行号1,列1: ^
XML Parsing Error: no element found Location: moz-nullprincipal:{ba25ef4a-f215-486e-b965-e70714c5af31} Line Number 1, Column 1: ^
我在这里做错了,我只是想不通,因为提琴手给了我很好的答复?
What I am doing wrong here, I just cannot figure out, because fiddler is giving me good response?
推荐答案
您声明ResponseFormat = WebMessageFormat.XmlResponseFormat
,返回的不是xml,而是字符串.它期望第一个字符为<.但是它是一个S,这就是为什么它在位置1找不到元素
You state ResponseFormat = WebMessageFormat.XmlResponseFormat
and what you return is not xml but a string. It expects the first character to be a < but its a S thats why its not finding an element at position 1
这篇关于Ajax调用宁静的WS总是出错(提琴手得到了很好的响应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!