jQuery ajax responseText '未定义'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5460275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:12:22  来源:igfitidea点击:

jQuery ajax responseText 'undefined'

jqueryajax

提问by daviiies

I've got some jQuery which posts two variables to a php script. The PHP is very simple and simply returns a string based on what it's given, i.e. 'update successful' which I'd like to use in some way on the page.

我有一些 jQuery,它将两个变量发布到 php 脚本。PHP 非常简单,只是根据给定的内容返回一个字符串,即“更新成功”,我想在页面上以某种方式使用它。

The first time I click I get an alert saying 'undefined', any further clicks and everything works fine. Pretty sure I'm not far off but I can't work out this problem!

第一次单击时,我收到一条警告,提示“未定义”,再单击一次,一切正常。很确定我离得很远,但我无法解决这个问题!

I've used firebug and the data is posted and the correct response is received on all attempts.

我使用了 firebug 并发布了数据,并且在所有尝试中都收到了正确的响应。

$(document).ready(function(){

    $('#updatehol').click(function() {
    additions  = $('#additions').attr('value');
    deductions   = $('#deductions').attr('value');
    datastring = 'additions='+ additions +'&deductions='+ deductions;

    $.ajax({
                type: "POST",
                data: datastring,
                url: "doadjust.php",
                complete: function(data) {
                alert(data.responseText);
                }
               });
    });
});

回答by daviiies

Sorted it. Needed async: falseoption in the Ajax function.

排序了。async: falseAjax 函数中需要的选项。

$.ajax({
    type:     "POST",
    data:     datastring,
    url:      "doadjust.php",
    dataType: "html",
    async:    false,
    success:  function(data) {
        alert(data);
    }
});

回答by alexl

What do you get with just an alert(data)??

你只用一个alert(data)??

btw it's success not complete:

顺便说一句,它的成功还没有完成:

        $.ajax({
                type: "POST",
                data: datastring,
                url: "doadjust.php",
                success: function(data) {
                   alert(data);
                }
               });
    });

回答by Syed Minhaj Uddin

This works for me.

这对我有用。

var min = $.ajax(
        {
            type: 'POST',
            url: 'LineChart.asmx/LineChrt',
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function(data) {
                // alert("SUCESS");
            },
            error: function(xhr, status) {
                alert("hatada:" + xhr.responseXML);
            },
            onComplete: function(data) {

            }

回答by takeit

You should add jsontype, ex.:

您应该添加json类型,例如:

$('#test').on('click', '.testclass', function(event){
    $.post($(this).attr('href'), 
     function(data) {
        alert(data.text);
    }, 'json');
}); 

回答by Chuck D

This is an old question but I just ran into the same problem and found the answer in the jQuery docs.

这是一个老问题,但我遇到了同样的问题,并在 jQuery 文档中找到了答案。

responeText and responseXml are only polulated when using dataType: text or xml. If you use any other data types it will be passed as the first param in your success callback.

responeText 和 responseXml 仅在使用 dataType: text 或 xml 时被污染。如果您使用任何其他数据类型,它将作为成功回调中的第一个参数传递。

From the jQuery Docs:

来自 jQuery 文档:

Data Types

The $.ajax() function relies on the server to provide information about the retrieved data. If the server reports the return data as XML, the result can be traversed using normal XML methods or jQuery's selectors. If another type is detected, such as HTML in the example above, the data is treated as text.

Different data handling can be achieved by using the dataType option. Besides plain xml, the dataType can be html, json, jsonp, script, or text.

The text and xml types return the data with no processing. The data is simply passed on to the success handler, either through the responseText or responseXML property of the jqXHR object, respectively.

数据类型

$.ajax() 函数依赖于服务器来提供有关检索到的数据的信息。如果服务器将返回数据报告为 XML,则可以使用普通的 XML 方法或 jQuery 的选择器遍历结果。如果检测到另一种类型,例如上面示例中的 HTML,则数据将被视为文本。

使用 dataType 选项可以实现不同的数据处理。除了纯 xml 之外,数据类型还可以是 html、json、jsonp、脚本或文本。

text 和 xml 类型返回未经处理的数据。数据分别通过 jqXHR 对象的 responseText 或 responseXML 属性简单地传递给成功处理程序。