简体   繁体   English

Javascript / JQuery AJAX无法解析AJAX响应

[英]Javascript/JQuery AJAX can't parse AJAX response

I know there is are a tone of post on this but I can't seems to find them. 我知道在这上面有一些贴子,但是我似乎找不到它们。 Here is where I am being stupid: 这是我愚蠢的地方:

I have an AJAX resqueting for an id and a integer (will be a margin apply on the DOM later on) 我有一个ID和一个整数的AJAX重新排列(稍后将在DOM上使用余量)

Here is the request: 这是请求:

$(function(){
    $("#divAccordion").accordion({
    //Accordion options here, not relevant
    }).sortable({
    //Other sortable options here, not relevant
    //This is the important part:
        update:
            function(event, ui){
                var data = $(this).sortable('toArray');
                $.ajax({
                    url:"prc.php",
                    type:"POST",
                    dataType:'JSON',/*--Added this also--*/
                    data:{sort:data},
                    contentType : "application/x-www-form-urlencoded;charset=UTF-8"
                }).done(
                    function(margin){
                        console.log(margin);//This log [sebastien20140804130001:45, sebastien20140804130002:30]
                        for(key in margin){
                            console.log(key+"=>"+margin[key]);
                        }
                    }
                );
            }
    });
});

Here is what I get in the console: 这是我在控制台中得到的:

[sebastien20140804130001:45, sebastien20140804130002:30]
0=>[
1=>s
2=>e
3=>b
4=>a
...

See the problem? 看到问题了吗? My response (margin) is a string and my for loop goes through each char. 我的响应(保证金)是一个字符串,我的for循环遍历每个字符。 I was expecting something like this instead: 我期待这样的事情:

[sebastien20140804130001:45, sebastien20140804130002:30]
sebastien20140804130001=>45
sebastien20140804130002=>30

Just a side note I can't know I much 'key/value' pairs I will receive neither if the IDs will follow each other (numeric order) ([ID:MARGIN_TO_APPLY]) 只是一个侧面说明,如果ID彼此跟随(数字顺序),我将不会收到太多的“键/值”对([ID:MARGIN_TO_APPLY])

What I am aiming at is to be able to loop through each 'key/value' pair and apply something like this: 我的目标是能够遍历每个“键/值”对并应用如下内容:

$('#'+key).css('margin-left', value);

If you guys needs any other information please ask in the comments I will provide them. 如果你们需要任何其他信息,请在评论中提出,我将提供给他们。

Thanks! 谢谢!

--EDIT-- - 编辑 -

Here is the server side code: 这是服务器端代码:

echo '{';/*--was [--*/
sortRN('rn_GLOBAL', 0);
echo '}';/*was ]--*/

function sortRN($dep, $i)
{
    if(isset($_POST['sort'][$i]) && $_POST['sort'][$i] != '')
    {
        $cnSort = new cConnexion('***', '***', '***', '***');
        if($cnSort->DBConnexion())
        {
            $query = "UPDATE ***.reunion SET rn_DEP = :DEP WHERE REPLACE(REPLACE(REPLACE(CONCAT('rn_', rn_SAMAORG, rn_DTSTART), ' ', ''), ':', ''), '-', '') = :ID";

            while(isset($_POST['sort'][$i]) && preg_match('#^.+[0-9]{4}$#', $_POST['sort'][$i]))
            {
                if($i > 0)
                {
                    echo ',';/*--Added this so there wouldn't always be a coma at the end--*/
                }

                switch($dep)
                {
                    case 'rn_GLOBAL':
                        $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                        $rsSort = $cnSort->SecureExecute($query, $params);
                        if($rsSort)
                        {
                            echo '{"'.$_POST['sort'][$i].'":15}';/*--Changed every line like this to add {} and also removed the coma at the end--*/
                        }
                    break;
                    case 'rn_JRT':
                        $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                        $rsSort = $cnSort->SecureExecute($query, $params);
                        if($rsSort)
                        {
                            echo '"'.$_POST['sort'][$i].'":30,';
                        }
                    break;
                    case 'rn_ACHAT':
                        $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                        $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_ADM':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_ASC':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_COMP':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_DIRECTION':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_INDUS':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_INFO':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_PROD':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_RED':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_STD':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_TEST':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_VENTE':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_CUSTOME':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_CRP':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":30,';
                    }
                break;
                case 'rn_FAB':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_IC':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_MAG':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_PRD':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_SERV':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_VE':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":45,';
                    }
                break;
                case 'rn_CAL':
                    $params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);

                    $rsSort = $cnSort->SecureExecute($query, $params);
                    if($rsSort)
                    {
                        echo '"'.$_POST['sort'][$i].'":30,';
                    }
                break;  
            }
            $i++;
        }

        $cnSort->DBDeconnexion();
        if(isset($_POST['sort'][$i]) && $_POST['sort'][$i] != '')
        {
            $ii = $i + 1;
            sortRN($_POST['sort'][$i], $ii);
        }
    }
    else
    {
        echo $cnSort->m_log->getMessageFR();
    }
}
}

Side note #2: The PHP script is updating the DB with the position of the JQuery sortable items then it echo the element ID and the value for the border. 旁注2:PHP脚本使用JQuery可排序项目的位置更新数据库,然后回显元素ID和边框的值。

--EDIT2-- -编辑2--

I changed some of the code see code above / --Highlighted this way-- / 我更改了一些代码,请参见上面的代码/- 以这种方式突出显示- /

  1. The response is not a valid JSON (Specification: http://json.org/ ). 该响应不是有效的JSON(规范: http : //json.org/ )。 It sends an array, but each element is not a valid object. 它发送一个数组,但是每个元素都不是有效的对象。 It should be something like this: 应该是这样的:

    [{"sebastien20140804130001" : 45}, {"sebastien20140804130002": 30}] [{“ sebastien20140804130001”:45},{“ sebastien20140804130002”:30}]

  2. Once you receive the response, you have to do a JSON.parse(response) to convert to a JSON object, or use the dataType: 'json' configuration so that jQuery does it for you automatically. 收到响应后,您必须执行JSON.parse(response)转换为JSON对象,或使用dataType: 'json'配置,以便jQuery自动为您完成。

This is terribly wrong and you should by any means to structure your data as valid json and follow Rajkumar Madhuram's answer, but here you go, as an exsercise in regEx: 这是非常错误的,您应该通过任何方式将数据构造为有效的json并遵循Rajkumar Madhuram的回答,但是在这里,您可以在regEx中进行练习:

function(margin){
    var values = margin.match(/(\w+:\d+)+/);
    var keyValues = [];
    for (var i=0 ; i< values.length; i++) {
        var keyValue = values[i].split(":");
        keyValues.push({ key: keyValue[0], value: keyValue[1] });
    }
    // now you have an array with key-value pairs;
}

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

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