Note that there are some explanatory texts on larger screens.

plurals
  1. POSend Json via $.load() of jQuery in a GET request to Django
    primarykey
    data
    text
    <p>Happy coding weekend to everyone!!!.</p> <p>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:</p> <pre><code>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'); }); } </code></pre> <p>The "item" json obj received was made out of a Model of Django using</p> <pre><code>jsonToSendToAjax = serializers.serialize('json', obj) </code></pre> <p>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: </p> <pre><code>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) </code></pre> <p>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.</p> <p>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: <a href="http://api.jquery.com/load/" rel="nofollow">http://api.jquery.com/load/</a> the $.load() method works as follow: .load( url, [data], [complete(responseText, textStatus, XMLHttpRequest)] )</p> <p>The POST method is used if data is provided as an object; otherwise, GET is assumed.</p> <p>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: </p> <pre><code>{"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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload