【问题标题】:400(Bad Request) when a Json object is posted发布 Json 对象时出现 400(错误请求)
【发布时间】:2016-10-19 06:59:24
【问题描述】:

我正在对 wcf 服务电子邮件进行发布请求,这是我的对象,它具有主题和正文属性。 当我尝试进行 ajax 调用时,我收到 400 Bad Request 错误,这是我下面的代码。我不知道如何将对象放入 stringify 函数中。

{
  "email": {
     "subject": "b",
     "body": "d"
  }
}

 $('#Button1').click(function() {
            var email = {
                subject: $("#Text1").val(),
                body: $("#Text1").val(),

            }

            $.ajax({
                url:"http://localhost:29143/Restwebservice.svc/sendmail",
                type: 'post',
                cache: false,
                contentType: "application/json; charset=utf-8",
                dataType: 'json',

                //data: JSON.stringify(email)
                data: JSON.stringify(email),
                success: function (data) {
                    $('#target').html(data.msg);
                }
            });

【问题讨论】:

  • 直接调用 wcf 服务是否有效?
  • 其实我在一个网站上调用这个服务
  • 不,我的意思是,如果您使用 Fiddler 或类似工具调用 localhost:29143/Restwebservice.svc/sendmail,您会得到结果吗?
  • getpostman.com 也适用于手动端点测试。
  • 你的 sendmail 方法签名是什么样子的?发布。

标签: c# jquery json ajax stringify


【解决方案1】:

当 json 无效时会出现错误请求,这里出现此错误是因为您有一个名为“email”的对象,然后在 Button1 上单击您正在定义名为“email”的新变量。在这里,您不是引用先前的电子邮件对象,而是创建新变量并在“var email”没有成员时尝试为其成员分配值。尝试做这样的事情

Object email = new Object();
email.subject =$("#Text1").val();
email.body = $("#Text1").val();

现在对这个“电子邮件”对象进行字符串化,但要确保在服务器端接收到与电子邮件相同的对象,因为如果发送和接收对象不匹配,则会引发错误请求。

【讨论】:

    【解决方案2】:

    您还没有向我们展示sendmail 方法,但是我假设从 ajax 调用的数据结构来看应该是这样的:

    [DataContract]
    public class EmailEntity
    {
        [DataMember]
        public string subject { get; set; }
        [DataMember]
        public string body { get; set; }
    }
    

    现在sendmail 方法看起来像这样

    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public void sendmail(EmailEntity emailentity)
    {
      ............
    }
    

    请注意,请求类型是包装的,因此在从 ajax 发布数据时,您最好先创建一个 emailentity 对象,该对象将用作包装器对象。

    var emailentity = {};
    emailentity.subject = "Test subject";
    emailentity.body = "Test body";
    
    var jsonObj = JSON.stringify(emailentity);
    var dataToSend = '{"emailentity":'+jsonObj+'}';
    

    这个dataToSend 现在将通过 ajax 发布。

    $.ajax({
                type: "POST",
                async: false,
                data: dataToSend,
                url: "../path/myService.svc/sendmail",
                contentType: "application/json; charset=utf-8",
                dataType: "json",          
                success: function () {
                    alert("success");
                },
                error: function () {
                    alert("Error");
                }
            });
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-30
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2018-06-30
      相关资源
      最近更新 更多