为什么此条件在JS中不正确 [英] Why this condition is not correct in JS

查看:113
本文介绍了为什么此条件在JS中不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有返回以下JSON的代码:

I have code that returns the following JSON:

{"code_retour":-1,"texte_retour":"Le lien de parent\u00e9 est inconnu"}

在我的JS代码中,我遇到了返回true的条件:

In my JS code, I have this condition which returns true:

    if (reponse.code_retour != 0) {

这将返回false:

if (reponse.code_retour == -1) {

我缺少一些东西. 为什么都不都返回true?

这是我的代码,以获取更多详细信息:

Here is my code for more details :

$.ajax({
       type: 'POST',
       url: $('#url_for_ajax').val()+'/post_formulaire_responsables_personne',
       data: $('#form_responsable').serialize(),
       success: function(reponse) {

        // reset des erreurs
        $('#form_responsable :input').parent().parent().removeClass('has-error has-feedback');
        $('#form_responsable :input').nextAll('span,small').remove();
        $("#reponse-serveur").hide();
console.log(reponse);           
        // il y a des erreurs dans le formulaire
        if (reponse.code_retour == -1) {
            alert('I am here');             
            var arr = reponse.texte_retour;
            if (!jQuery.isEmptyObject(arr)){
                    var errorString = '<ul>';
                    $.each(arr, function(index, value)
                    {
                        if (value.length != 0) {errorString += '<li>' + value + '</li>';}

                        $(':input[name="' + index + '"]').parent().parent().addClass('has-error has-feedback');
                        $(':input[name="' + index + '"]').after('<span class="glyphicon glyphicon-warning-sign form-control-feedback" aria-hidden="true"></span><small class="text-danger">'+value+'</small>');
                    });
                    errorString += '</ul>';
            }

           $("#reponse-serveur").html(errorString);
           $("#reponse-serveur").show();


           } else {

推荐答案

这两种情况都返回true. 这里是一个例子.也许这些值不是您所期望的?

Both cases return true. Here is an example. Perhaps the values are not what you are expecting?

如果您使用的是Chrome,则可以使用内置的调试工具来查看发生了什么情况.

If you are using Chrome, you can use the built-in debugging tools to check out what's going on.

  • Here is a primer on debugging with Google Chrome.
  • Here is one for Firefox.

在这种情况下,当您尝试评估reponse real 值时,我会尝试找出它是什么.有几种方法可以做到这一点:

In this case, I would try to figure out what the real value of reponse is, when you're trying to evaluate it. There are a few ways to do this:

您应该检查以确保您的答复是您所期望的.试试看:

You should check to make sure your response is what you're expecting. Try these things out:

文档

这使您可以在浏览器的控制台中添加一条消息.在这种情况下,console.log(reponse);应该可以.从这里您将看到类似这样的内容:

This allows you to put a message into your browser's console. In this case, console.log(reponse); should be fine. From here you would see something like this:

在这里您可以看到reponse.code_retour实际上是-1(如示例中)还是其他.

Here you can see if reponse.code_retour is actually -1 (as in the example) or something else.

文档

您可以简单地在代码中的某个位置放置debugger;一词,如果您具有调试工具(如前面的链接所述),则可以在该时刻暂停代码.在这里,您可以看到reponse.code_retour的实际值是什么.

You can simply put the word debugger; somewhere in your code, and if you have the debugging tools (as described in the earlier links) open it pause the code at that instant. Here you can see what the real value of reponse.code_retour is.

断点很酷.

从本质上讲,它们使您可以通过在特定时刻停止代码来暂停时间",并且您可以随意在此处四处移动.

They let you essentially "pause time" by stopping the code at a particular moment, and you are free to move around here.

您可以使用以下按钮向前跳跳入跳出功能范围:.所有这些都可能在较早的文档中进行了解释.

You can skip forward, jump into, and jump out of function scopes, with these buttons: . All of this is probably explained in the earlier documents.

这些本质上是断点:

您还可以使用浏览器内置的调试工具添加断点".只需转到来源"标签,然后单击您要在以下位置停止的行号:

You can also add "breakpoints" using the debugging tool built into your browser. Just go to the sources tab, and click the line number you want to stop at:

首先,您应该检查要发送到服务器的内容!代码有可能没有达到您的期望.

You should check what you are sending to your server in the first place! It's possible the code isn't doing what you expect.

为此,建议您提取要传递到$.ajax中的所有内容,并将其存储在单独的变量(例如,ajaxConfig)中.然后,您可以使用较早的一种方法来确保发送到服务器(ajaxConfig.data)的数据是您希望发送的数据.

To do this, I'd recommend pulling out everything you are passing into $.ajax, and storing it in a separate variable (for example, ajaxConfig). Then, you can use one of the methods earlier to ensure the data you send to the server (ajaxConfig.data) is what you are expecting to send.

示例代码:

var ajaxConfig = {
   type: 'POST',
   url: $('#url_for_ajax').val()+'/post_formulaire_responsables_personne',
   data: $('#form_responsable').serialize(),
   success: successCallback
};

// Here you can see exactly what you are sending to the server!
console.log("ajaxConfig: ", ajaxConfig.data);

$.ajax(ajaxConfig);

这篇关于为什么此条件在JS中不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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