所以我正在使用jQuery进行Ajax调用,并且它在所有桌面浏览器和在android中的chrome上运行良好但是它在iOS上的Safari中无效吗?
$(document).ready(function(){
(function($){
function processForm( e ){
var callid = $('.callid').val();
var pin = $('.pin').val();
var urlFinal = callid+'/'+pin;
$.ajax({
url: 'http://URLHERE/getHash/' + urlFinal,
dataType: 'text',
type: 'get',
contentType: 'application/x-www-form-urlencoded',
success: function( data, textStatus, jQxhr ){
var urlResponse = JSON.parse(data);
console.log("WORKS! " + urlResponse.streamFileUrl);
$('.overlay').show();
playVideo(urlResponse);
},
error: function( jqXhr, textStatus, errorThrown ){
console.log('INCORRECT DETAILS ');
$('.incorrect').show()
}
});
e.preventDefault();
}
$('#form').submit( processForm );
})(jQuery);
})
答案 0 :(得分:0)
至少从多余的额外加载处理程序中解包您的代码,在加载处理程序中只需要很少的IIFE
$(document).ready(function(){ // load handler
(function($){ // remove
将preventDefault移动到提交将停止的位置,如果其余代码失败并删除不必要的垃圾:
dataType: 'text', // jQUery will figure this out based on response
type: 'get', // default
contentType: 'application/x-www-form-urlencoded', // only needed for POST
var urlResponse = JSON.parse(data); // if text is left out, JSON is returned
这是一个更短更清洁的版本
$(function() { // shorter version of $(document).ready(function
$('#form').on("submit", function(e) {
e.preventDefault(); // belongs HERE
var callid = $('.callid').val();
var pin = $('.pin').val();
var urlFinal = callid + '/' + pin;
$.ajax({
url: 'http://URLHERE/getHash/' + urlFinal,
success: function(data, textStatus, jQxhr) {
console.log("WORKS! " + data.streamFileUrl); // is already JSON
$('.overlay').show();
playVideo(urlResponse);
},
error: function(jqXhr, textStatus, errorThrown) {
console.log('INCORRECT DETAILS ');
$('.incorrect').html(errorThrown).show()
}
});
});
});