2

Happy coding weekend to everyone!!!.

I'm stuck trying to send a JSON object via $.load() of jQuery, i want to send it with the GET method, this is the code that i have in my javascript code, I attached the Ajax request that receives the JSON Object for clarity:

function ajaxLoadClasses() {
    $.ajax({
        url: 'load_classes/',
        type: 'GET',
        dataType: 'json',
        success: function(json) {
            $.each(json, function(iterator,item) {
                              loadViaGet(item);
            });
        },
        error: function(xhr, status) {
            alert('Sorry, there was a problem!');
        },
        complete: function(xhr, status) {},
    });
}


function loadViaGet(item) {
    $div = $('div.myClass');
    //Here is where I'm stuck, I'm not sure if this is the way to send the JSON obj
    $div.load('thisAppURL/?json=' + encodeURIComponent(item), function() { 
        alert('Load was performed');
    });
}

The "item" json obj received was made out of a Model of Django using

jsonToSendToAjax = serializers.serialize('json', obj)

And I don't think that I'm using the correct methods in my Django to deserialize the JSON object or to convert the JSON object into a Python object so I can handle it in my view and send it to a template:

def popUpForm(request):
    jsonData = request.GET['json']

    deser = serializers.deserialize('json', jsonData)

    #This could be another way to convert the JSON object to a Python Object
    #pythonObj = simplejson.loads(jsonData)

    return render_to_response('class_pop_up_form.html', deser)

It will be very helpful if someone can help me with this!! I'm really struggling with it but I don't find the right way to do it.

EDIT 1 : I want to send the JSON object via the GET with the $.load() function, not with the POST method,as I read in the jQuery api: http://api.jquery.com/load/ the $.load() method works as follow: .load( url, [data], [complete(responseText, textStatus, XMLHttpRequest)] )

The POST method is used if data is provided as an object; otherwise, GET is assumed.

EDIT 2: Forget about sending the json object via the GET method, now I'm using the POST method, but now I don't figure out how to use that json object in my Django View.py, don't know if i need to deserialize it or not, the format of the json object that I'm using is the following:

{"pk": 1,
"model": "skedified.class",
"fields": {
      "hr_three": null,
      "group": 1,
      "name": "Abastecimiento de agua",
      "day_three": null,
      "day_one": "1 , 3",
      "hr_one": "10+/3",
      "online_class": null,
      "teacher_name": "Enrique C\\u00e1zares Rivera /   ",
      "day_two": null,
      "class_key": "CV3009",
      "hr_two": null }
}
ElHacker
  • 1,687
  • 17
  • 18

2 Answers2

2

This isn't how jQuery suggests you should send the data and it's probably not a good idea to do it this way either. Your url gets very ugly and long very quick if you add the json string to it like that.

Use the second argument for $.load; "data" (see http://api.jquery.com/load/) instead. So something like

$div.load('thisAppURL', {"json": encodeURIComponent(item)});

Also, if you want to trace the output, I'd sugest using the third argument, the callback function, and use console instead of alert. You can get the actual return from the server that way too. So you'd get something like:

$div.load(
  'thisAppURL',
  {"json": encodeURIComponent(item)},
  function(response, status, xhr){
    console.log(response);
  }
);
Jasper Kennis
  • 3,225
  • 6
  • 41
  • 74
  • Your suggestion works for sending the json object to the server and I can acces it by using `request.POST['json']`, but I still can not deserialize the object in the Django views, Do you have any idea of how to do it? – ElHacker Aug 27 '11 at 03:58
  • Yeah I've had trouble with that too sometime, I'll try to figure out how I solved that. – Jasper Kennis Aug 27 '11 at 08:42
  • Try json.loads and let me know if it worked! (See http://docs.python.org/library/json.html) – Jasper Kennis Aug 27 '11 at 08:49
  • IT WORKS! instead of `json.loads()` I used `simplejson.loads()`, and in the jQuery code instead of `encodeURIComponent(item)` I used `JSON.stringify(item)` thank you for your help! – ElHacker Aug 27 '11 at 20:21
  • Whaha, seems like you only needed some hints;) Good luck with your project! – Jasper Kennis Aug 28 '11 at 03:33
0

the question was not clear to me but you can send json via load as the second argument

$div = $('div.myClass');
//Here is where I'm stuck, I'm not sure if this is the way to send the JSON obj
$div.load('thisAppURL/?json=' + encodeURIComponent(item),{"name":"john","age":"20"}, function() { 
    alert('Load was performed');
});

for converting javascript array to json see this answer Convert array to JSON

and for deserializing json in django Django Deserialization

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101