problem with calling remote ASMX using jQuery

前端 未结 4 1892
一向
一向 2021-01-21 10:52

Been trying my best to understand this correctly. What is the difference between an XML, SOAP and JSON response? And how does one know how to call a web service whose response i

4条回答
  •  感动是毒
    2021-01-21 11:38

    [Edit] Another thing you could try is to change the dataType in the JQuery call to "xml". If that doesn't work, you could make your own proxy Web-Service that calls the remote one, and then return the data in a JSON format.

    I suspect the problem is in the server side code. I'm not sure if this will work for you but here is some working code that shows JQuery calling my WebMethod. Hopefully you can compare this with yours and get it working. Let us know what the solution is. I hope this helps.

    [Server Code]
    
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        [ScriptService]
        public class ForumService : System.Web.Services.WebService
        {
    
            [WebMethod]
            [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
            public VoteCastResult CastQuestionVote(string itemID, QAForum.Bll.VoteType voteType)
            {
                try
                {
                    User usr = SecurityHelper.GetOrCreateUser();
                    Guid question = new Guid(itemID);
                    return new QuestionVoteController().CastQuestionVote(usr, question, voteType);
                }
                catch (Exception ex)
                {
                    return new VoteCastResult(VoteCastStatusType.otherIssue, 0, ex.Message);
                }
            }
        }
    
    
    [JQuery Code]
    
        function AFTopicCastVote(clickContext, itemID, voteDirection, voteMethod)
        {
              $.ajax({
              type: "POST",
              contentType: "application/json; charset=utf-8",
              url: (AFServiceUrl + voteMethod),
              data: "{'itemID': '" + itemID + "','voteType': '" + voteDirection + "'}",
              dataType: "json",
              success: function (data, textStatus) {
                                   AFTopicProcessVoteResult(clickContext, data.d);
                                    //alert("data : " + data.d);
                                },
    
              error: function (  XMLHttpRequest, textStatus, errorThrown) 
              {
                alert("error casting vote: " + errorThrown);
              }
            });    
        }
    

提交回复
热议问题