JQuery Ajax请求收到

我正在向同一个文档发送一段日期,一切正常,直到我想将响应值与字符串进行比较。 所以这是我的JQuery:

$.ajax({ type: 'POST', dataType : 'html', cache: false, data: {serienummer: serial}, success: function(response) { console.log(response); if ('success' == response){ //Do something } else { //Do something else } }, error: function(xhr, status, error){ alert('error: ' + error); } }); 

我将数据发布到同一页面并将其与数据库中的数据进行比较,如果存在比较则返回“成功”,否则返回“错误”。

但是如果我在console.log()响应中得到以下结果:

 success 

为什么这不仅仅是’成功’还是’错误’? 如何解决此问题以比较响应? 它与dataType有关吗?

提前致谢

你可以像这样添加成功function的状态:

 success: function( data, textStatus, jqXHR ) 

更多信息: http : //forum.jquery.com/topic/jquery-ajax-textstatus-documented

那是因为你还没有在你的ajax中指定url

 $.ajax({ type: "POST", url: "url", data: "data to be post", contentType: "application/json; charset=utf-8", dataType: "html", success: function(result){ alert(result.d); console.log(result); } }); 

出口(); 在处理所有工作的PHP代码的最后! 我希望这可以帮助别人

在那个ajax调用中你可以设置dataType:’html’删除它并尝试它。 你可以设置数据类型:html ,这样你就可以得到像这样success响应success

转换为答案

您正在将页面发布到自身,并从您所在的页面返回HTML。 您需要添加一个出口或将html包装在else中

 if (isSet($_POST)) { echo "success"; exit(); } ?>  

只为该调用返回字符串“success”或创建另一个php:

 $.ajax({ url="someprocess.php"