我知道有关于此的帖子,但我似乎无法找到它们。这是我愚蠢的地方:
我有一个用于id和整数的AJAX resqueting(稍后将在DOM上应用保证金)
以下是请求:
$(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]);
}
}
);
}
});
});
以下是我在控制台中获得的内容:
[sebastien20140804130001:45, sebastien20140804130002:30]
0=>[
1=>s
2=>e
3=>b
4=>a
...
看到问题?我的响应(margin)是一个字符串,我的for循环遍历每个char。 我期待这样的事情:
[sebastien20140804130001:45, sebastien20140804130002:30]
sebastien20140804130001=>45
sebastien20140804130002=>30
只是一个侧面说明我无法知道我的关键/价值'如果ID将相互跟随(数字顺序),我将不会收到([ID:MARGIN_TO_APPLY])
我的目标是能够遍历每个键/值'配对并应用这样的东西:
$('#'+key).css('margin-left', value);
如果您需要任何其他信息,请在我将提供的评论中提问。
谢谢!
- 编辑 -
这是服务器端代码:
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();
}
}
}
附注2:PHP脚本正在使用JQuery可排序项的位置更新数据库,然后它回显元素ID和边框值。
- EDIT2 -
我更改了一些代码,看到上面的代码/ - 以这种方式突出显示 - /
答案 0 :(得分:2)
响应不是有效的JSON(规范:http://json.org/)。它发送一个数组,但每个元素都不是有效的对象。它应该是这样的:
[{" sebastien20140804130001" :45},{" sebastien20140804130002":30}]
收到回复后,您必须执行JSON.parse(response)
转换为JSON对象,或使用dataType: 'json'
配置,以便jQuery自动为您执行此操作。
答案 1 :(得分:0)
这是非常错误的,你应该以任何方式将你的数据结构化为有效的json并遵循Rajkumar Madhuram的答案,但是你在这里,作为regEx中的exex:
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;
}