电子邮件AJAX发布到网络API [英] email AJAX POST to web api

查看:139
本文介绍了电子邮件AJAX发布到网络API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有客户端的功能,例如:

I have client side function such as:

        var sendEmail = function (msgData) {
            jQuery.support.cors = true;
            var test = msgData;
            $.ajax({
                url         : "Api/EmailData/Send/{}",
                type        : "POST",
                data        : JSON.stringify(msgData),
                contentType : "application/json; charset=utf-8",
                success     : function (data) {

                },
                error: function (error) {

                }
            });
    };

像这样网络控制器的方法:

Web controller method like so:

        [Route("Api/EmailData/Send/{clientEmail}"), HttpPost]
        public HttpResponseMessage ContactUs(string clientEmail)
        {
            EmailBody emailbody = JsonConvert.DeserializeObject<EmailBody>    (clientEmail);

            var result = MyDataRepository.ContactUs(emailbody);
            return Request.CreateResponse(result == false ?     HttpStatusCode.ExpectationFailed : HttpStatusCode.OK);
        }

我的数据与预期我已经检查了邮件类。但是我仍然获得了404未找​​到。如果我跑提琴手,我收到了响应状态为204号Content.I还包括在我的提琴手头:

My data matches the email class expected I have checked. However i still get a 404 not found. If i run in FIDDLER i get a Response Status as 204 No Content.I also include in my Fiddler header:

内容类型:应用程序/ JSON

Content-type:application/json

我怎样才能使它从我的JS客户端和提琴手工作两者。任何指针AP preciated。请注意我已经包括允许接入控制 - 原产地为我的数据站点在IIS中的客户端。我也试着从我的JS客户端删除JSON.stringify所建议的地方esle。不知道我错过了什么。任何帮助。谢谢

How can i make it work both from my JS client side and fiddler. Any pointers appreciated. Note i have included "Allow-Access-Control-Origin" for my Client on the Data Site in IIS. I have also tried removing JSON.stringify from my JS Client as suggested somewhere esle. Not sure what I am missing. Any help. Thanks

推荐答案

它看起来像你可能有一个错误的URL。如果我的怀疑是正确的/ {}在URL的末尾可能是你的问题的根源。

It looks like you may have a malformed url. If my suspicions are correct the "/{}" at the end of url may be the source of your problems.

下面的{}是罚款,要在API控制器端

Here the {} are fine as you are declaring this on api controller end

    [Route("api/stuff/data/{id:int}")]
    public Data GetDataById(int id)
    {
        // your code here

        return data;
    }

但是,在你的Ajax调用只需把该URL没有花括号

But in your ajax call just pass the url without the curly braces

       $.ajax({
            type: 'POST',
            url: "Api/EmailData/Send",
            data: JSON.stringify(actionData),
            traditional: true,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { },
            error: function (e) {  }
        });

这篇关于电子邮件AJAX发布到网络API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆