Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

JSON response w/ onComplete
  • I want to be able to pass back a json array from my uploader.php script and then have that array available in my onComplete method but it seems to just be passing back a plain string. In my php file I am doing something like

    echo json_encode(array("status"=>"1","message"=>"message goes here"));

    but when I do an alert of the response variable in my onComplete method, I get {"stats":"1","message":"message goes here"}.

    What am I doing wrong? How can I make jQuery see that this is a JSON array and not a plain string?
  • \"onComplete\": function(a, b, c, d, e) {
    data = eval(\"(\" + d + \")\");
    if(d && !data.msg){
    $(\"#response\").html(d);
    }
    if(data.msg){
    $(\"#response\").html(data.msg);
    }
    },


    I remain with using d as I tested for other errors.
    You can pass general echo back and have d output it or use json.
  • you have a type as well

    stats


    should be

    status
  • Hi!

    I had the same problem and the following onComplete handler solved it:

    onComplete: function(event, ID, fileObj, response, data){
    var data = $.parseJSON(response);
    if (data.type == 'notification') {
    alert('Do something');
    }
    },
  • don't use eval,use parseJSON
  • It works perfect for me.
    I'm using an app in django and there it return a json and code python looks like:

    def add_image_to_alias(request):
    destino_tmp='/tmp/'
    for multipart_name in request.FILES.keys():
    multipart_obj = request.FILES[multipart_name]
    destino_file = destino_tmp+str(multipart_obj)
    try:
    archivo_tmp = open(destino_file,'wb+')
    for chunk in multipart_obj.chunks():
    archivo_tmp.write(chunk)
    archivo_tmp.close()
    except OSError,error:
    return render_to_response('error.html',{'error':str(error)}, context_instance=RequestContext(request))
    except IOError, error:
    return render_to_response('error.html',{'error':str(error)}, context_instance=RequestContext(request))
    except Exception, error:
    return render_to_response('error.html',{'error':str(error)}, context_instance=RequestContext(request))
    satus='ok'
    response_dict={"response":"ok"}
    return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')

    I suppose that the main problem is the return of example in php.
    Greets,
    Roberto