Best way to return JSON data for AJAX

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Best way to return JSON data for AJAX

Dave Nadler

Hi All - I'm looking for guidance on good ways to return JSON data for use in a web page via AJAX.
I found a couple of threads discussing using SSI for this but I'm unclear the overall approach.
For this application I don't need to worry about session IDs - just need to return some JSON
data, in multiple chunks to avoid chewing up enormous memory in prep.
Thanks in advance for any guidance (or even better an example!),
Best Regards, Dave

References:
https://lists.gnu.org/archive/html/lwip-users/2017-11/msg00137.html
http://lwip.100.n7.nabble.com/LWIP-HTTPD-SSI-MULTIPART-and-concurrent-HTTP-requests-td31266.html


-- 
Dave Nadler, USA East Coast voice (978) 263-0097, [hidden email], Skype 
 Dave.Nadler1

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Best way to return JSON data for AJAX

Marco Lazzaroni
Hi,
  If I understand correctly, I'm doing something similar here.

I have created a json file as below:
{
    "ssl_ok_count": "<!--#SslOkCount-->",
    "ssl_err_count": "<!--#SslErrCount-->",
    "ssl_log": "<!--#SslLog-->"
}

Then in the javascript you have to do something like this:
/* this is from the XMLHttpRequest */ 
obj = JSON.parse(stripAwayComments(this.responseText));
document.getElementById("id_ssl_ok_count").innerHTML = obj.ssl_ok_count;
document.getElementById("id_ssl_err_count").innerHTML = obj.ssl_err_count;
document.getElementById("id_ssl_log").innerHTML = obj.ssl_log;

This is the related function, called above
function stripAwayComments(str) {
return str.replace(/<\!--.*?-->/g, "");
}

This is because I was not aware of LWIP_HTTPD_SSI_INCLUDE_TAG, see the related comments in httpd.h and httpd_opts.h.
With LWIP_HTTPD_SSI_INCLUDE_TAG set at 0, you can change the line above to 
obj = JSON.parse(this.responseText);

I think that I'm facing the same problem you have because my "char log_memory[10240]" array should be appended to my SslLog tag. So, I don't think that setting LWIP_HTTPD_MAX_TAG_INSERT_LEN to 10240 is the correct approach :-)
LWIP_HTTPD_SSI_MULTIPART has been written for this need, now I just have to understand how to implement the suggestion you linked (https://lists.gnu.org/archive/html/lwip-users/2017-11/msg00151.html).

Hope this helps 
Marco


Il giorno mer 15 apr 2020 alle ore 18:17 Dave Nadler <[hidden email]> ha scritto:

Hi All - I'm looking for guidance on good ways to return JSON data for use in a web page via AJAX.
I found a couple of threads discussing using SSI for this but I'm unclear the overall approach.
For this application I don't need to worry about session IDs - just need to return some JSON
data, in multiple chunks to avoid chewing up enormous memory in prep.
Thanks in advance for any guidance (or even better an example!),
Best Regards, Dave

References:
https://lists.gnu.org/archive/html/lwip-users/2017-11/msg00137.html
http://lwip.100.n7.nabble.com/LWIP-HTTPD-SSI-MULTIPART-and-concurrent-HTTP-requests-td31266.html


-- 
Dave Nadler, USA East Coast voice (978) 263-0097, [hidden email], Skype 
 Dave.Nadler1
_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Best way to return JSON data for AJAX

Dave Nadler
In reply to this post by Dave Nadler
Thanks Marco and Adam for the suggestions.
This works great:
- create a fixed local JSON file with SSI-include syntax for the values to substitute (lwIP's included httpd processes JSON type for SSIs).
- #define LWIP_HTTPD_SSI 1
- #define LWIP_HTTPD_SSI_MULTIPART 1
- #define LWIP_HTTPD_SSI_INCLUDE_TAG 0 // Otherwise, SSI substitution leaves original tag in JSON/HTML as a comment

I did not address trying to create a much larger file dynamically, though a short JSON could be expanded arbitrarily if set up as above.
Also I didn't address context-identification which may be required for multiple connections and multi-part responses;
this part isn't obvious...

Thanks again for the help,
Best Regards, Dave

On 4/15/2020 12:17 PM, Dave Nadler wrote:

Hi All - I'm looking for guidance on good ways to return JSON data for use in a web page via AJAX.
I found a couple of threads discussing using SSI for this but I'm unclear the overall approach.
For this application I don't need to worry about session IDs - just need to return some JSON
data, in multiple chunks to avoid chewing up enormous memory in prep.
Thanks in advance for any guidance (or even better an example!),
Best Regards, Dave

References:
https://lists.gnu.org/archive/html/lwip-users/2017-11/msg00137.html
http://lwip.100.n7.nabble.com/LWIP-HTTPD-SSI-MULTIPART-and-concurrent-HTTP-requests-td31266.html


-- 
Dave Nadler, USA East Coast voice (978) 263-0097, [hidden email], Skype 
 Dave.Nadler1

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Best way to return JSON data for AJAX

Trampas Stern
What I did was use the fs.c example, and when user requests a file like '/json' I create a string on micro with the json and send back using the fs.c api.   The restriction here is that I send a JSON no bigger than my string size, which is statically allocated. 

To send json to the micro I have used http POST messages and when I get the application/json content tag I then store the JSON file on SD card (as they can be 100k or more) and then once the post is done I parse the JSON on the micro.  To parse the large json file I wrote a custom parser which limits the memory usage during parsing. 

I also found that my uploading files was really slow, due to number of buffers, so I had to increase buffers.  However the maximum bit rate I am getting is around 1mbps, which is not great, but good enough for what I need. 

Trampas



On Mon, Apr 20, 2020 at 2:59 PM Dave Nadler <[hidden email]> wrote:
Thanks Marco and Adam for the suggestions.
This works great:
- create a fixed local JSON file with SSI-include syntax for the values to substitute (lwIP's included httpd processes JSON type for SSIs).
- #define LWIP_HTTPD_SSI 1
- #define LWIP_HTTPD_SSI_MULTIPART 1
- #define LWIP_HTTPD_SSI_INCLUDE_TAG 0 // Otherwise, SSI substitution leaves original tag in JSON/HTML as a comment

I did not address trying to create a much larger file dynamically, though a short JSON could be expanded arbitrarily if set up as above.
Also I didn't address context-identification which may be required for multiple connections and multi-part responses;
this part isn't obvious...

Thanks again for the help,
Best Regards, Dave

On 4/15/2020 12:17 PM, Dave Nadler wrote:

Hi All - I'm looking for guidance on good ways to return JSON data for use in a web page via AJAX.
I found a couple of threads discussing using SSI for this but I'm unclear the overall approach.
For this application I don't need to worry about session IDs - just need to return some JSON
data, in multiple chunks to avoid chewing up enormous memory in prep.
Thanks in advance for any guidance (or even better an example!),
Best Regards, Dave

References:
https://lists.gnu.org/archive/html/lwip-users/2017-11/msg00137.html
http://lwip.100.n7.nabble.com/LWIP-HTTPD-SSI-MULTIPART-and-concurrent-HTTP-requests-td31266.html


-- 
Dave Nadler, USA East Coast voice (978) 263-0097, [hidden email], Skype 
 Dave.Nadler1
_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users