jQuery cannot get a success function working
I am having trouble getting a success: function(){}
working - the ajax code I am using is this:
jQuery.ajax({
url: 'http://127.0.1/process_form.php'+data,
data: data,
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpcallback"
});
data
is a simple string and is being passed by jsonp as when complete will pass data cross domains. I know about security with a GET and that Curl would be better, but this is a simple "predefined" string with no user input. The data is just the text from one of 4 links. i.e. "yes" "No" "Don't know" "made this one up" There is actually no need for a form either.
I get all the "data" into the DB but what I can't get to happen is the 开发者_如何学编程"next" bit based on the success. If I do a simple success: function(){}
nothing happens (with that function) regardless of "what" e.g. an alert etc.
What should (and does of sorts) happen is the data is posted to the DB, a jQuery.getJSON
statement then queries the DB and prints out ALL the data i.e. all entered rows, not just the last.
When I say "sort of works" the jQuery.getJSON returns the data, but sometimes "misses" doing it until the next time data is sent. It seems to me all to do with timing because if I wrap the "print" function inside a setInterval()
the whole thing works fine. Just I don't really need the setInterval
to contunually work.
For example:
jQuery('.addinput').live('click',function(){
var fldID = new Date().getTime();
var fldType = jQuery(this).html().toLowerCase();
var data ='';
data += '?fldID='+fldID;
data += '&fldType='+fldType;
data += iptime;
jQuery.ajax({
url: 'http://127.0.1/process_form.php'+data,
data: data,
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpcallback"
});
jQuery('.loader').fadeIn('slow');
setInterval(function() { fn_form(iptime); }, 3000 );
jQuery('.loader').fadeOut('slow');
return true;
});
This works OK but I really don't want the fn_form(iptime)
function to be continually refreshed.
What I'd rather see is something like this:
jQuery('.addinput').live('click',function(){
var fldID = new Date().getTime();
var fldType = jQuery(this).html().toLowerCase();
var data ='';
data += '?fldID='+fldID;
data += '&fldType='+fldType;
data += iptime;
jQuery.ajax({
url: 'http://127.0.1/process_form.php'+data,
data: data,
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpcallback",
success: function(){
// run fn_form(iptime)
}
});
You can use this alternative solution to jQuery's implementation of JSONP which simplify JSONP calls.
jQuery-JSONP features:
1- error recovery in case of network failure or ill-formed JSON responses,
2- precise control over callback naming and how it is transmitted in the URL,
3- multiple requests with the same callback name running concurrently,
4- two caching mechanisms (browser-based and page based),
5- the possibility to manually abort the request just like any other AJAX request,
a timeout mechanism.
精彩评论