无法从jquery访问数组

时间:2016-10-19 03:06:22

标签: javascript php jquery

好的,在我之前的问题之前,我已经对我的代码进行了一些修改,但不知怎的,我仍然落后。

这是我创建的jquery

$(document).ready(function()
    {
        var array_ids = [];
        $('.add').click(function()
        {
            array_ids.push($(this).parent().siblings('.row_id').html().trim());
            alert(array_ids);    
        });

        $('.show').click(function(e)
        {
            //e.preventDefault();
            var jsonString = JSON.stringify(array_ids);
            $.ajax(
            {
               method: 'POST',
               url: 'addsale.php',
               data: {data : jsonString},
               cache: false,
               dataType: "json",
               success: function()
               {
                 console.log(data.reply);
                alert(data.reply);
               } 
            });
        });
    });

并且addsale.php

if(isset($_POST['push'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
 echo $d;
 }
 }

有人能告诉我访问数组时缺少什么,并将addsale.php中的html获取到当前页面吗?

4 个答案:

答案 0 :(得分:1)

$(document).ready(function()
{
    var array_ids = [];
    $('.add').click(function()
    {
        array_ids.push($(this).parent().siblings('.row_id').html().trim());
        alert(array_ids);    
    });

    $('.show').click(function(e)
    {
        //e.preventDefault();
        //prefer parse function
        var jsonString = JSON.stringify(array_ids);
        $.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {"data" : jsonString},
           cache: false,
           dataType: "json",
           //e is the response text from your PHP code
           success: function(e)
           {
             //I don't know why this code
             //console.log(data.reply);
            //alert(data.reply);
           } 
        });
    });
});
在PHP代码中

尝试

if(isset($_POST['data'])) //tried it commenting also!
{
   $data = array();
   $data = json_decode(stripslashes($_POST['data']));
   foreach($data as $d){
       echo $d;
   }
 }

答案 1 :(得分:0)

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) { NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ; [self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]]; } } - (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url { @try { ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) { if (asset == nil) return; ALAssetRepresentation *assetRep = [asset defaultRepresentation]; NSString *fileName = [assetRep filename]; NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate]; NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM-dd-yyyy"]; NSLog(@"%@",[dateFormatter stringFromDate:myDate]); ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error) { }; ALAssetsLibrary *library = [ALAssetsLibrary new]; [library assetForURL:url resultBlock:resultblock failureBlock:failureblock]; } @catch (NSException *exception) { } } 回显并不能很好地处理json响应!您应该在响应ajax foreach函数后循环该json ...

<强> addsale.php

success

访问ajax中的json resposne

if(isset($_POST['data'])) //tried it commenting also!
{
   $data = array();
   $data = json_decode(stripslashes($_POST['data']));
   echo json_encode($data);
 }

答案 2 :(得分:0)

我认为你有一切正确,但遗漏了一些正确的var Child = React.createClass({ render: function() { return <div ref={function(el){this.props.whatElementAmI(el)}.bind(this)} >Oh My!</div> } }); var Parent = React.createClass({ whatElementAmI: function(el) { console.log(el.nodeName); // "DIV" }, render: function() { return <div><Child whatElementAmI={this.whatElementAmI.bind(this)} /></div> } }); ,你还应该添加如下内容 -

data: {data: ... }

并在PHP代码中尝试解码不是json格式的数组, 你的代码只需要像这样 -

 data : {data : jsonString, 'push':'push'},

答案 3 :(得分:0)

您的成功函数中没有data参数。修改您的addsale.php

$(document).ready(function()
{
    var array_ids = [];
    $('.add').click(function()
    {
        array_ids.push($(this).parent().siblings('.row_id').html().trim());
        alert(array_ids);    
    });
    $('.show').click(function(e)
    {
        //e.preventDefault();
        var jsonString = JSON.stringify(array_ids);
        $.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {data : jsonString},
           cache: false,
           dataType: "json",
           success:function(data)
           {
             console.log(data.reply);
             alert(data.reply);
           } 
        });
    });
});

addsale.php

<?php
if(isset($_POST['data'])) //tried it commenting also!
{
$data=json_decode(stripslashes($_POST['data']));
echo json_encode(['reply'=>$data]);
exit();
}