简体   繁体   English

AJAX返回奇怪的HTML文件而不是JSON

[英]AJAX returning a weird HTML file instead of JSON

I'm using AJAX with jQuery trying to get a response from a database, but I'm not even able to get a proper JSON response. 我正在将AJAX与jQuery结合使用,试图从数据库中获取响应,但是我什至无法获得正确的JSON响应。 The response that comes, instead of JSON, is an HTML code from a XAMPP page (I'm using it to test locally). 响应而不是JSON,是来自XAMPP页面的HTML代码(我正在使用它来进行本地测试)。

This is the JS code with jQuery and AJAX: 这是jQuery和AJAX的JS代码:

 $('#salvar').click(function(e) { e.preventDefault(); var dados = $('buscaCep').serialize(); $.ajax({ url: "resposta.php", data: dados, type: "POST", dataType: "json", success: function(response) { console.log(response); }, error: function(jqxhr, textStatus, errorThrown) { console.log("jqXHR: ", jqxhr); console.log("textStatus: ", textStatus); console.log("errorThrown: ", errorThrown); } }); return false; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 

And this is the 'resposta.php' file: 这是“ resposta.php”文件:

 <?php $arr = array(); $arr['cep'] = $_POST['cep']; $conn = ... $sql = ... if ( $conn->ping() ) { $arr['connected'] = true; } else { $arr['connected'] = false; } if ( $conn->query($sql) === TRUE ) { $arr['xstatus'] = "Created" ; $arr['id'] = $conn->insert_id; } else { $arr['xstatus'] = "Error" ; $arr['message'] = $conn->error; } $myJSON = json_encode($arr, JSON_HEX_QUOT | JSON_HEX_TAG); echo $myJSON; ?> 

And the weird response I get is this: 我得到的怪异反应是:

AJAX中的HTML响应

I don't even know where this is coming from. 我什至不知道这是从哪里来的。 Its an HTML file from XAMPP, the package I'm using to make a local server, but I have no idea why. 它是XAMPP的HTML文件,这是我用来制作本地服务器的软件包,但我不知道为什么。 Tried to look for similar problemas but haven't found anything. 试图寻找类似的问题,但没有发现任何东西。

This is what I do (or something like it) 这就是我所做的(或类似的事情)

  $.ajax({
    url: "resposta.php",
    data: dados,
    type: "POST",
    dataType: "json",
    always: function(jqxhr, textStatus, errorThrow) {
           try {
                //in the case of a "real" error throw and catch it (DRY like)
                if(errorThrow) throw errorThrow; 
                //in the case we can't parse the JSON response catch that too
                var data = JSON.parse(jqxhr.responseText);

                 /*... other code ...*/

           } catch(e) {
                 console.log("jqXHR: ", jqxhr);
                 console.log("textStatus: ", textStatus);
                 console.log("errorThrown: ", e);
           }
    }
});

Always returns even if you do happen to get a 404 or any error, but if your not sending the application/json headers your browser may not parse the JSON data on it's own, but it doesn't matter because you can try to parse it and then when that fails you can log the errors. 即使您碰巧遇到404错误或任何错误,也总是返回,但是如果您不发送application/json标头,则浏览器可能无法自行解析JSON数据,但这没关系,因为您可以尝试解析它然后失败时,您可以记录错误。

You could probably come up with a way to know if it's the page that responds to a 404 (and add that in the catch block, for an even more sensible error). 您可能想出了一种方法来确定页面是否响应404(并将其添加到catch块中,以获得更明智的错误)。 What I mean by 404 here is not an actual 404 but a unresolvable route in a Framework (I didn't really pay attention to what you are using), some of them may set the proper HTTP status. 我在这里所说的404不是一个实际的404,而是框架中无法解决的路由(我并没有真正注意您使用的是什么),其中一些可能设置了正确的HTTP状态。 Some might just show some kind of search page and act like everything is ok. 有些人可能只是显示某种搜索页面,并且一切正常。

I had that happen before (a long time ago), where I get a 200 response no matter what and some garbage HTML search page for the response. 我曾经(很久以前)发生过这种情况,无论如何我都会得到200个响应,并为响应添加了一些垃圾HTML搜索页面。

Hope it helps. 希望能帮助到你。

I didn't tested your code. 我没有测试您的代码。 But with your Error Message, I can say something ... 但是随着您的错误消息,我可以说些什么...

  1. Your AJAX code may be above the jQuery Declaraction. 您的AJAX代码可能在jQuery声明之上。
  2. Use Latest Version of JQuery 使用最新版本的JQuery
  3. If you are using partial views, then correctly wrap AJAX inside your script section 如果您使用局部视图,则将AJAX正确包装在脚本部分中

 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#salvar').click(function(e) { e.preventDefault(); var dados = $('buscaCep').serialize(); $.ajax({ url: "resposta.php", data: dados, type: "POST", dataType: "json", success: function(response) { console.log(response); }, error: function(jqxhr, textStatus, errorThrown) { console.log("jqXHR: ", jqxhr); console.log("textStatus: ", textStatus); console.log("errorThrown: ", errorThrown); } }); return false; }); 

You can try this : 您可以尝试以下方法:

Replace 更换

$myJSON = json_encode($arr, JSON_HEX_QUOT | JSON_HEX_TAG);

with

$myJSON = json_encode($arr, JSON_FORCE_OBJECT);

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

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