简体   繁体   English

jQuery-如何进行Ajax POST调用并附加数据

[英]jQuery - How to make an ajax POST call and append the data

I want to use the ajax() function to send a JSON variable and an integer to a page and append the retrieved data. 我想使用ajax()函数向页面发送JSON变量和整数,并附加检索到的数据。 This is where I'm at at the moment: 这是我目前所在的位置:

$.ajax({
    type: "POST",
    dataType: "json",
    url: "get_theme.php",
    data: {themestack:themes, iterator:theme_max_iterator},
    success: function(data){
        alert('Items added');
        $("#themes").append(data);
        ++IFRAMES_IN_DOM;
        ++theme_max_iterator;
    },
    error: function(e){
        alert(e.message);
    }
});  

themes is the JSON variable and theme_max_iterator is int . themesJSON变量,而theme_max_iteratorint Currently I'm getting an alert of simply "undefined" so I'm wondering what is wrong? 目前,我收到有关"undefined"的警报,所以我想知道哪里出了问题?

EDIT: 编辑:

In the log file I can see an error from get_theme.php : 在日志文件中,我可以从get_theme.php看到错误:

PHP Warning:  json_decode() expects parameter 1 to be string, array given

And the line that caused it is: 导致它的行是:

$obj = $_POST['themestack'];
$json_data = json_decode($obj, true);

Which is strange since the JSON variable themestack is JSON . 这很奇怪,因为JSON变量themestackJSON

I think you are getting an array in json_decode() try to encode json first then decode it like, 我认为您在json_decode()中得到一个array ,尝试先encode json进行encode json然后再将其decode

$obj = $_POST['themestack'];
if(is_array($obj)){// check if obj is an array then encode it first
   $obj = json_encode($obj);
}
$json_data = json_decode($obj, true);

Or before passing your data make it json like, 或者在传递data之前将其json

data: {themestack:JSON.stringify(themes), iterator:theme_max_iterator},

It looks like you are hitting the alert in your error: callback... I would suggest looking at the Request/response to see what the server returned. 看来您是在错误中发出警报:回调...我建议您查看请求/响应以查看服务器返回了什么。

Alternatively you could change the alert(e.message) to alert(JSON.stringify(e)) to find out what it tells you. 另外,您可以将alert(e.message)更改为alert(JSON.stringify(e))来了解其内容。 But first thing mentioned is likely to more informative and closer to the issue than this later one. 但是提到的第一件事可能比后面的那件事更具信息性,并且更接近问题。

if your AJAx is expecting JSON data, You must echo using "json_encode" in you php 如果您的AJAx需要JSON数据,则必须在PHP中使用“ json_encode”进行回显

    <?php
    $result = "blah blah"
    echo json_encode($result );
    ?>

I think your themes is a array. 我认为您的themes是一个数组。 So, when you use $_POST['themestack'] in PHP, you got a array. 因此,当您在PHP中使用$_POST['themestack']时,会得到一个数组。 And the jason_decode(...) function needs a JSON string as a argument. 而且jason_decode(...)函数需要一个JSON字符串作为参数。

Maybe you should change the following line to: 也许您应该将以下行更改为:

data: {themestack:JSON.stringify(themes), iterator:theme_max_iterator},

Or maybe you don't need jason_decode() and the $_POST['themestack'] object is what you want. 或者,也许您不需要jason_decode(),而您想要的是$_POST['themestack']对象。 You can treat it as an array and access it like $_POST['themestack'][0] . 您可以将其视为数组,并像$_POST['themestack'][0]一样访问它。

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

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