简体   繁体   English

JS中未显示Ajax响应

[英]Ajax response not showing in JS

I have an AJAX script like this 我有这样的AJAX脚本

function savetoDB(inp) {
    var userID = (FB.getAuthResponse() || {}).userID;

    jQuery.ajax({
        url: URL,
        type: 'GET',
        data:{ 'input': JSON.stringify(inp) },
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        async: true,
        success: function(msg) {
            console.log(msg);
            alert(msg);
        }
    });
}

The problem I am facing is that the php returns (echo's) some string , but I am unable to see it in alert. 我面临的问题是php返回(回显)某些字符串,但是我无法在警报中看到它。 I used the inspect element and Inside inspect element I can see the Response, and also the status is 200, what could be the reason that it is not showing the response in alert? 我使用了检查元素和内部检查元素,可以看到响应,并且状态为200,这可能是它未在警报中显示响应的原因吗?

You are returning a JSON object, so it won't show in alert, which only displays strings. 您将返回JSON对象,因此它不会显示在仅显示字符串的警报中。 Convert it to a string first: 首先将其转换为字符串:

success: function(msg) {
    console.log(msg);
    alert(JSON.stringify(msg));
}

将“ URL”替换为您要从中提取数据的文件的实际URL,否则您将收到空响应。

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

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