【问题标题】:Not getting ajax response on the server even with 200 status即使状态为 200,也无法在服务器上获得 ajax 响应
【发布时间】:2023-02-01 12:02:50
【问题描述】:

我在模糊输入上添加了一个 ajax 调用,然后单击提交按钮以验证电子邮件是否存在。

代码很简单,只需检查现有数据并返回 true false 作为响应。

在本地它工作得很好,但在客户端的服务器上,ajax 没有按预期响应。响应状态为 200,但在浏览器控制台上的响应预览中没有任何内容。为了更清楚添加图片。

阿贾克斯代码:

jQuery.ajax({
            url : "{{ path('sales_account_email_exist') }}",
            type : "POST",
            cache : false,
            data : {email:email_val,user_id:$("#user-id").val()},
            success : function(data){
              $(".loader").hide();
              var obj = $.parseJSON(data);
              if(obj===false)
              { 
                $("#duplicate_email_exist").css("display","inline-block");
                errorMessage = '<i class="warning sign icon"></i>' + email_val + ' : Email already exists in the system.';
                $("#duplicate_email_exist").html(errorMessage);
                email_exist_valid = false;
              }
              else
              {
                $("#duplicate_email_exist").css("display","none");
                $("#duplicate_email_exist").html("");
                email_exist_valid = true;
              }
            }
         })

Ajax后端代码:

/**
     * @Route("/exist-email", name="sales_account_email_exist",methods={"GET","POST"})
     */
    public function emailExist(Request $request): Response
    {
        $email = $request->get('email');
        $user_id = $request->get('user_id');

        if (isset($email) && !empty($email)) {
            if (isset($user_id) && !empty($user_id)) {
                $query = $this->em->createQueryBuilder()
                    ->select('u.id', 'u.email')
                    ->from('App\Entity\User', 'u')
                    ->andWhere('u.email =\'' . str_replace("'", '', $email) . '\'')
                    ->andWhere('u.id !=' . $user_id)
                    ->getQuery();
            } else {
                $query = $this->em->createQueryBuilder()
                    ->select('u.id', 'u.email')
                    ->from('App\Entity\User', 'u')
                    ->where('u.email = :email')
                    ->setParameter('email', $email)
                    ->getQuery();
            }

            $email_exist = $query->getArrayResult();
            if (!empty($email_exist)) {
                return new response('false');
            } else {
                return new response('true');
            }
        }
        return new response('true');
    }

【问题讨论】:

  • 尝试查看服务器中的日志,并查看网络服务器/PHP 的配置以允许您查看重新请求中的错误

标签: php jquery ajax symfony


【解决方案1】:

在success: 之后添加error: 声明,这可能有助于发现问题。

jQuery.ajax({
            url : "{{ path('sales_account_email_exist') }}",
            type : "POST",
            cache : false,
            data : {email:email_val,user_id:$("#user-id").val()},
            success : function(data){
              $(".loader").hide();
              var obj = $.parseJSON(data);
              if(obj===false)
              { 
                $("#duplicate_email_exist").css("display","inline-block");
                errorMessage = '<i class="warning sign icon"></i>' + email_val + ' : Email already exists in the system.';
                $("#duplicate_email_exist").html(errorMessage);
                email_exist_valid = false;
              }
              else
              {
                $("#duplicate_email_exist").css("display","none");
                $("#duplicate_email_exist").html("");
                email_exist_valid = true;
              }
            },
            // add this
            error: function (request, status, error) {
                alert(request.responseText); // <-- your code will be here
            }
         })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多