How do I use Ajax with jQuery in Perl?
If I call this function directly it works fine, yet if I call it from Javascript using an ajax call it wont work correctly.
I found out the reason for this, it w开发者_如何学编程as cause I was using $(document).ready(function ..) where I should call the functions directly after the ajax returns the results.
At minimum, your Perl script needs to output the following kind of text before any other output:
Content-Type: text/html
Note that there must be an empty line after the Content-Type
line.
If you fail to do this, the server will not send the text to the JavaScript. Typically it would trip the line
alert('There was a problem with the request.');
in your JavaScript. If you examine the value of the variable .status
, it should be equal to 500, an error code which indicates your script failed.
So confusing, your perl code just outputs html without any headers (like content-type). Your javascript doesn't seem to use jQuery, and none of the referred IDs appear anywhere in your example.
Were you looking to use load something like: $('#view_span').load('filename_ajax.cgi');
?
Or perhaps:
$('#view_span').load(
'filename_ajax.cgi',
{'target': escape(encodeURI('get_section'))},
alertViewResult);
Although it's a bit under-documented (at least for someone who isn't already intimately familiar with jQuery itself), have you taken a look at JQuery on CPAN?
精彩评论