简体   繁体   English

在C#中从$ .Post获得空响应

[英]Getting empty response from $.Post in c#

I am trying to call a rest service using $.Post(). 我正在尝试使用$ .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. 我的服务接受输入并以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. 但是,当我使用ajax发布调用Firefox浏览器中的Firebug来检查我的网络时,它没有任何响应。 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. 您可能会对XMLHttpRequests的相同原始策略犯规。 Basically ajax requests can only be made to the same scheme/host/port as the page running the Javascript. 基本上,只能向与运行Javascript的页面相同的方案/主机/端口发出ajax请求。

Do you really have an absolute URL hard coded in the $.post() call? 您真的在$.post()调用中硬编码了绝对URL吗? Switching to a relative URL would eliminate this possibility $.post("/SaveUserVote", ... , guaranteeing that the scheme, host, and port are the same. 切换到相对URL将消除这种可能性$.post("/SaveUserVote", ... ,从而确保方案,主机和端口相同。

Do you see the outgoing ajax POST request in the network tab of your browser dev tools? 您是否在浏览器开发工具的“网络”标签中看到传出的ajax POST请求? (F12 in IE, Ctrl-Shift-I in chrome). (在IE中为F12,在Chrome中为Ctrl-Shift-I)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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