简体   繁体   中英

Getting empty response from $.Post in c#

I am trying to call a rest service using $.Post(). The syntax to call my service is below:

  $.post("http://MyIp/SaveUserVote", { "PostId": 1, "UserId": 3 }, function (result) {
                            alert("success");
                        });

My service takes the input and return the result in JSON. Please check my Rest Service below:

 public object Post(SaveUserVote request)
    {

        if (string.IsNullOrEmpty(request.PostId.ToString()))
            throw new ArgumentNullException("Required", "PostId cannot be null");

        try
        {
            return SaveUserVote(request);
        }
        catch (Exception ex)
        {
            objResult._result = "NotSuccess";
            dicResult.Add("UserVote", objResult);

            string output = JsonConvert.SerializeObject(dicResult);
            return output;
        }
    }

      public string SaveUserVote(SaveUserVote userPost)
    {
                Result objResult = new Result();
                Dictionary<string, Result> dicResult = new Dictionary<string, Result>();
              var objVote = new PostVote
            {
                PostId = userPost.PostId,
                UserId = userPost.UserId,
             };
            _dbcontext.PostVotes.InsertOnSubmit(objVote);
            _dbcontext.SubmitChanges();


        objResult._result = "Success";
        dicResult.Add("UserVote", objResult);

        string output = JsonConvert.SerializeObject(dicResult);
        return output;
    }

            public class Result
    {
        public string _result { get; set; }
    }

This service is working fine when I hit this form the server side. But it gives no response when I check my Net in the Firebug on Firefox browser while calling it using ajax post. Can anyone please get me out of this issue. I think that I am doing some silly syntax error while calling or missed out parameter that will be required for getting response. Thanks

ANY QUICK HELP ??

You might be falling foul of the same origin policy for XMLHttpRequests. Basically ajax requests can only be made to the same scheme/host/port as the page running the Javascript.

Do you really have an absolute URL hard coded in the $.post() call? Switching to a relative URL would eliminate this possibility $.post("/SaveUserVote", ... , guaranteeing that the scheme, host, and port are the same.

Do you see the outgoing ajax POST request in the network tab of your browser dev tools? (F12 in IE, Ctrl-Shift-I in chrome).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM