2

I want to use jquery to implement ajax response from perl script on the server.But when I use such code, I got nothing. I can get the result from the url in a browser, so I don't know where is wrong.

$(document).ready(function(){
$("button").click(function(){

$.ajax({
  'type': 'GET',
  'url': 'http://XXXX/cgi-bin/XXX.pl?title=dd',
  'async': true,
  'success': function(data) {
        alert(data);
  }
});

});
});
2
  • $("button") you mean all button element has this function when clicked or you meant something like $("#button") ? Commented Oct 26, 2012 at 23:44
  • 1
    If you use Chrome's Developer Tools - Network Panel, you can review exactly what ajax requests are made, and what the response was developers.google.com/chrome-developer-tools/docs/network Commented Oct 26, 2012 at 23:46

2 Answers 2

2

If url field contain http://server.domain/ and if you try to access a server from a webpage located elswhere than http://server.domain/ (like in your desktop for sample, accessed via file:///), probably you need to add a field Access-control-allow-origin: * in the header sent by your perl CGI:

#!/usr/bin/perl -w

use CGI;
my $q=new CGI;

print $q->header(-type=>"text/plain", -Access_Control_Allow_Origin=>"*")."Answer";

Take care of security considerations...

Sign up to request clarification or add additional context in comments.

Comments

0

Try running the following diagnostic :

$(document).ready(function(){
    $("button").click(function(){
        alert('click happened');
        $.ajax({
            'type': 'GET',
            'url': 'http://XXXX/cgi-bin/XXX.pl?title=dd',
            'async': true,
            'success': function(data, textStatus, jqXHR) {
                alert('ajax success: ' + [data, textStatus]);
            },
            'error': function(jqXHR, textStatus, errorThrown){
                alert('ajax error: ' + [textStatus, errorThrown]);
            }
        });
    });
});

Somewhere amongst the alerts (or lack of them) you should get a clue as to to what is failing.

3 Comments

it shows "ajax error: error". when I change the async to false, it shows "[Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: code.jquery.com/jquery-1.8.2.js :: <TOP_LEVEL> :: line 8416" data: no]"
is it because what i returned from the perl is an html page with a table?
Ultimately, you will want an HTML fragment (eg just the table) rather than a whole page. But that doesn't explain why you currently get an ajax error. The most likely cause is that you are trying to query a different domain from that of the current page (ie a cross-domain request). If so, then you might like to try this hack which claims to allow cross-domain GET requests. Definitely keep async:false - true is not supported in some browsers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.