使用API​​ 2的js代码 - 不使用Sweet Alert警告正确的消息? [英] js code with API 2 - don't alert the right message using Sweet Alert ?

查看:55
本文介绍了使用API​​ 2的js代码 - 不使用Sweet Alert警告正确的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i创建了一个js代码,用于从API控制器发布数据,并且我将控制器代码返回0或1或2并设置我的JS代码to:



  //   /< reference path =sweet-alert.min.js/>  


$( document ).ready( function (){
$( #subscribeBtn)。click( function (){
var textvalue = $( #mail)。val();
$ .ajax({
url: api / Newsletter
type : POST
data:{ :textvalue},
contentType: application / x-www-form-urlencoded
成功:功能(数据){

$( #msg)。text(data) ;

if (data = 0){
sweetAlert( 糟糕...... 之前已订阅过! 错误);
}

if (data = 1){
sweetAlert( 糟糕... 您应该向我们提供您的邮件 error);
}

if (data = 2){
sweetAlert( 干得好! 恭喜!,您的订阅已被接受! success);
}
},
错误: function (xhr,status,err){
alert(err);
}
});
});
});





查看代码:



< pre lang =HTML> < div class = form-inline >

< 输入 类型 = text id = mail class = form-control 占位符 = 在此处输入电子邮件.... / >
< 按钮 id = subscribeBtn class = btn btn-primary > 订阅< / button >
< label id = msg > < / label >
< / div >





控制器代码:



  public   string 发​​布([FromBody]  string  mail)
{
try
{
if (!string.IsNullOrEmpty(mail))
{
if string .Compare( abc@some.com,mail)== 0
{
return 0;
}
其他
{
返回 2;
}
}
return 1;
}
catch (例外)
{
var responseMsg = Request.CreateResponse(HttpStatusCode.InternalServerError);
throw new HttpResponseException(responseMsg);
}
}





运行标签msg时显示从控制器返回的正确数字0 或1或2取决于条件,但我在所有情况下只收到此消息? :



 sweetAlert( 干得好! 恭喜!,您的订阅已被接受!,< span class =code-string>  success); 





这是错的,但我不知道为什么?我需要在JS代码中作为If条件工作



如果返回0告诉我:(现在我在标签msg中得到0并向我显示成功消息)< br $> b $ b

 sweetAlert( 糟糕...... 之前已经订阅过! 错误); 





如果返回1告诉我:(现在我在标签msg中显示1并向我显示成功消息)



 sweetAlert(< span class =code-string> 糟糕...... < span class =code-string>你应该向我们提供你的邮件,  error); 





如果返回2告诉我:(现在我在标签msg中显示2并向我显示成功消息)



 sweetAlert( 干得好! 恭喜! ,您的订阅被接受!  success); 

解决方案

document )。ready( function (){


#subscribeBtn ).click( function (){
var textvalue =


#mail)。val();


Hi
i have create an js code that Post data from API Controller and i det controller code to return "0" or "1" or "2" and set my JS code to:

/// <reference path="sweet-alert.min.js" />


$(document).ready(function () {
    $("#subscribeBtn").click(function () {
        var textvalue = $("#mail").val();
        $.ajax({
            url: "api/Newsletter",
            type: "POST",
            data: { "": textvalue },
            contentType: "application/x-www-form-urlencoded",
            success: function (data) {

                $("#msg").text(data);

                if (data = "0") {
                    sweetAlert("Oops...", "Already subscribed before!", "error");
                }

                if (data = "1") {
                    sweetAlert("Oops...", "you should provide us with your mail", "error");
                }

                if (data = "2") {
                    sweetAlert("Good job!", "Congrats! ,your subscription accepted!", "success");
                }
            },
            error: function (xhr, status, err) {
                alert(err);
            }
        });
    });
});



View Code:

<div class="form-inline">

                <input type="text" id="mail" class="form-control" placeholder="Type Email Here ...." />
                <button id="subscribeBtn" class="btn btn-primary">Subscribe</button>
                <label id="msg"></label>
            </div>



Controller Code:

public string Post([FromBody] string mail)
        {
            try
            {
                if (!string.IsNullOrEmpty(mail))
                {
                    if (string.Compare("abc@some.com", mail) == 0)
                    {                 
                        return "0";
                    }
                    else
                    {
                        return "2";
                    }
                }
                return "1";
            }
            catch (Exception)
            {
                var responseMsg = Request.CreateResponse(HttpStatusCode.InternalServerError);
                throw new HttpResponseException(responseMsg);
            }
        }



when run the label "msg" show me the right number returned from controller "0" or "1" or "2" depending on if condition but i got only this message in all cases ?! :

sweetAlert("Good job!", "Congrats! ,your subscription accepted!", "success");



that is wrong but i don't know why ? i need to work as If condition in the JS Code

if return "0" show me: ( now i got 0 in label msg and show me success message )

sweetAlert("Oops...", "Already subscribed before!", "error");



if return "1" show me: ( now i got 1 in label msg and show me success message )

sweetAlert("Oops...", "you should provide us with your mail", "error");



if return "2" show me: ( now i got 2 in label msg and show me success message )

sweetAlert("Good job!", "Congrats! ,your subscription accepted!", "success");

解决方案

(document).ready(function () {


("#subscribeBtn").click(function () { var textvalue =


("#mail").val();


这篇关于使用API​​ 2的js代码 - 不使用Sweet Alert警告正确的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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