简体   繁体   English

为什么jQuery ajax响应总是被填充

[英]Why is jQuery ajax response always filled

I noticed that the response of an ajax call with jQuery is never empty. 我注意到用jQuery进行ajax调用的响应永远不会为空。 I have the following: 我有以下几点:

$.ajax({
        type: "get",
        data: { data },
        url: "phpFile",
        datatype: 'text'
 }).done(function( response ) {             

        if (response.length > 2)
        alert(response);
    });

in the php file I only return text if there is an error with a query 在php文件中,如果查询有错误,我只会返回文本

echo mysqli_error($conn);

however even when I don't return anything the length is always 2 or greater, does someone knows why or how could I retrieve an empty text? 但是,即使我什么也不返回,长度总是2或更大,有人知道为什么或如何检索空文本吗?

I'm answering my question just to serve as reference for anyone seeing this. 我正在回答我的问题,仅供参考。

What I was trying to do was actually a bad practice and the best thing to do as Dipanwita Kundu mentioned is to return a proper success response, false or true with error message. 我试图做的实际上是一种不好的做法,而Dipanwita Kundu提到的最好的做法是返回正确的成功响应,无论false还是true并带有错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM