2013-03-08 58 views
1

在这里,我正在做一个JSONP请求,它将获得传入回调函数的对象字面值。jsonp:使用多个数组数据回调jQuery AJAX

我的问题:

1.从filename.php即时传递两个不同阵列的像:

 echo $_GET['jsoncallback'] . '(' . json_encode($manufacturers). ');';    
     echo $_GET['jsoncallback'] . '(' . json_encode($Stock). ');'; 

2.I'm只接收到一个阵列作为输出在一个时间。 3.我需要这两个数组。

    function onLoad(){ 
         var output = $('#product'); 
         $.ajax({ 
          url:'filename.php', 
          data : {type : 'details'}, 
          dataType: 'jsonp', 
          jsonp: 'jsoncallback', 
          timeout: 5000, 
          success:function(data){ 
           $.each(data, function(c,prdetail){ 

            if(prdetail.field_name=='' || prdetail.field_name==undefined) 
            { 
             var firstdata= '<h2 >'+prdetail.field_name1+ '</h2>'; 
             output.append(firstdata); 
            } 
            else 
            { 
             var secondata ='<h2 >'+prdetail.field_name2+ '</h2>'; 
             output.append(secondata); 
            } 
}); 
      } 
     }); 
    } 

FILENAME.PHP

 if($status=1) 
     { 
      $manufacturers_sql = mysql_query('select * FROM tablename'); 
    $rowcount = mysql_num_rows($result); 
    $records = array(); 
    $row = mysql_fetch_assoc($result); 
    $records[] = $row; 

    echo $_GET['jsoncallbacks'] . '(' . json_encode($records) . ');'; 
     } 
     else 
     { 
      $manufacturers_sql = mysql_query('select * FROM tablename'); 
    $rowcount = mysql_num_rows($result); 
    $records = array(); 
    $row = mysql_fetch_assoc($result); 
    $records[] = $row; 

    echo $_GET['jsoncallbacks'] . '(' . json_encode($records) . ');'; 
      } 
+0

那不是要去工作,你应该在1个单个对象结合阵列。因为echo'ing这两个阵列的结果是分开的,因为你要求一个带有2个函数(具有相同名称)的Javascript文件 – Rob 2013-03-08 11:04:26

+0

是否可以附加这些对象,我的意思是在php文件中给出不同的名称 – sherin 2013-03-08 11:05:28

回答

1

尝试

$response = array(); 

$response['manufacturers'] = $manufacturers; 
$response['Stock'] = $Stock; 

echo $_GET['jsoncallback'] . '(' . json_encode($response). ');'; 
+0

要做到这一点 – sherin 2013-03-08 11:08:54

+0

@NewBorn是啊..为什么.. ??你没有它。? – 2013-03-08 11:11:28

+0

是的dipesh,但它不工作.. – sherin 2013-03-08 11:18:12