ExtJS file upload ajax response strips HTML from inside string inside JSON - ajax

I have a form with a fileupload control in it, and I call form.submit with a success function.
My server side does the usual trick of setting the content type to text/html to get it to arrive in one piece.
In the success function, action.response.responseText does contain the JSON which I sent.
When it leaves the server, it looks like:
{
html: "<div>a div</div>"
}
When it arrives in the success function, the tags are missing. What's going on? Do I need to put some sort of html cdata wrapper around the entire response on the server to avoid this?

A string in a string in JSON. As long as it is well-formed you are allowed to put HTML in string values (making sure you escape quotes etc.).
It's probably the function you're using to insert the HTML that's stripping the tags.

Here's the situation. When you ask ExtJS or JQuery to do Ajax for a form with a file upload, it has to use an iframe. For the response to come back correctly, it has to be of content type text/html whatever is in it. So it has to have it's HTML characters escaped for HTML, which I accomplished with a function from CommonsLang.

Related

Is Ajax with JSON or Ajax with dojo are same

I am new in Ajax . Want to use Ajax with Json . I am searching the tutorial for this and i find this.
I want to ask is i am in right direction ?
Is both things are same Ajax with Json and Ajax with dojo?
Not the same
Ajax is a technology that send request and accept data asynchronously(do not need to reload page).
You can use JSON or XML to send the data or just use the string.
When you do the Ajax request, any data type(like array, object, number..) except string will lost their data type and become string, so If you want to reserve their type, you must use data transmit format like JSON and XML.
Dojo just a library which have easier method for doing Ajax. You also can use jQuery, Angular,..Whatever even JavaScript native XMLHttpRequest.

How to get a HTTPRequest JSON response without using any kind of template?

I am new to Django but i am advanced programmer in other frameworks.
What i intend to do:
Press a form button, triggering Javascript that fires a Ajax request which is processed by a Django View (creates a file) that return plain simple JSON data (the name of the file) - and that is appended as a link to a DOM-Element named 'downloads'.
What i achieved so far instead:
Press the button, triggering js that fires a ajax request which is process by a Django view (creates a file) that return the whole page appended as a duplicate to the DOM-Element named 'downloads' (instead of simple JSON data).
here is the extracted code from the corresponding Django view:
context = {
'filename': filename
}
data['filename'] = render_to_string(current_app+'/json_download_link.html', context)
return HttpResponse(json.dumps(data), content_type="application/json")
I tried several variants (like https://stackoverflow.com/a/2428119/850547), with and without RequestContext object; different rendering strats.. i am out of ideas now..
It seems to me that there is NO possibility to make ajax requests without using a template in the response.. :-/ (but i hope i am wrong)
But even then: Why is Django return the main template (with full DOM) that i have NOT passed to the context...
I just want JSON data - not more!
I hope my problem is understandable... if you need more informations let me know and i will add them.
EDIT:
for the upcoming questions - json_download_link.html looks like this:
Download
But i don't even want to use that!
corresponding jquery:
$.post(url, form_data)
.done(function(result){
$('#downloads').append(' Download CSV')
})
I don't understand your question. Of course you can make an Ajax request without using a template. If you don't want to use a template, don't use a template. If you just want to return JSON, then do that.
Without having any details of what's going wrong, I would imagine that your Ajax request is not hitting the view you think it is, but is going to the original full-page view. Try adding some logging in the view to see what's going on.
There is no need to return the full template. You can return parts of template and render/append them at the frontend.
A template can be as small as you want. For example this is a template:
name.html
<p>My name is {{name}}</p>
You can return only this template with json.dumps() and append it on the front end.
What is your json_download_link.html?
assuming example.csv is string
data ={}
data['filename'] = u'example.csv'
return HttpResponse(simplejson.dumps(data), content_type="application/json")
Is this what you are looking for?

Save original response data and replace them using MVC

I need to get response data (incuding html markup) on server and then replace them to anothe one and send to the client.
Is where the best way to do this?
Response.OutputStream is closed for reading.
So what can I use for reading data?
I need to get response data (incuding html markup) in controller
For obvious reasons that's not possible. The response HTML is generated when the view executes, at a much later stage after the controller action has finished executing.
If you want to modify the response HTML you could write a response filter.

How do use JSON body in REST requests?

I am developing an API using Codeigniter and Phils RESTserver. I know how to send the request body in normal Form format but how can I send it as a JSON object instead?
I do this now:
lastname=bond
I want to do this instead:
{"lastname" : "bond"}
I tried to just replace the Content type header from:
application/x-www-form-urlencoded
In to this:
application/json
This did not do anything. Codeigniter says the POST array is empty.
If I understood correctly you want to create a request that contains a JSON node inside the request body. Assuming this, I think it's not possible to create such a request using simple HTML form tags as your browser always will try to pack your input vars in a querystring like format.
You will need JavaScript to achive this (I think all popular libs like Scriptacoulous or JQuery comes with helper methods for this).

Any alternate of drupal_json for non-JSON output

In an AJAX call back in drupal it is normally recommended to use drupal_json() to send data to client. This function converts the raw data into JSON along with HTML encoding.
I want to send the HTML data without encoding to client.
for this I am using following code:
print $html_output;
exit(0);
Is there any recommended or best way in drupal to do so?
If you need to output only the HTML output returned from the menu callback, then the following code is the correct one:
print $html_output;
module_invoke_all('exit');
exit();
If you want your output to appear together the blocks Drupal normally output, then the code needs to be changed to the following:
return $html_output;
That will do the trick. Allthough you should invoke hook_exit first. However this is shortcutting the framework some what, it may work in simple cases but wont work for forms etc.
The only time I have used this method is if I am printing some data which is allready json encoded.

Resources