Ajax call restful WS is always getting error (fiddler gets the good response)

与世无争的帅哥 提交于 2019-12-11 11:27:31

问题


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

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.

My Ajax method:

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

});

});

I mozila firebug i section XML I get this:

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?


回答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



来源:https://stackoverflow.com/questions/13914604/ajax-call-restful-ws-is-always-getting-error-fiddler-gets-the-good-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!