我得到了这段代码:
function server_request(module,section,action,data) { data['module'] = module; data['section'] = section; data['action'] = action; var responsetxt = null; $.post('../application/server.php', data, function(data) { responsetxt = data; }); return responsetxt; }
它回归了null
吗?
我想要的是让server_request
函数返回responseText?但在某种程度上它不起作用,为什么?以及如何让它发挥作用?
我正在使用jquery将行加载到表中,我想知道如何逐个加载它们; 即第二个块只应在第一个块完成加载后开始加载.
我的表看起来有点像这样:
<table id="dynamicTable">
<thead>
<tr>
<th>Heading</th>
...
</tr>
</thead>
<tbody id="1">
</tbody>
<tbody id="2">
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我的jquery看起来有点像这样:
$("table#dynamicTable tbody").each(function() {
$(this).load("filethatgeneratestablerows.php");
});
Run Code Online (Sandbox Code Playgroud)
(它比这更复杂,因为它根据tbody id生成内容,还包括一个复选框系统来选择你想要影响哪些tbody元素,但是一切正常,所以我把它剥离了以使我的实际问题更明显!)
这是我的代码:
initHomeFeed: function(options) {
$.get('/feed_items', {}, function(data) {
console.log("Y U NO COME HERE")
console.log("hello");
Feed.feed_items_html = data.feed_items_html_array;
Feed.pseudo_feed_items_html = data.pseudo_feed_items_html_array
console.log(ordered_arr);
}, "json");
console.log("makehtml was called by inithomefeed")
Feed.makeHomeHTML();
Run Code Online (Sandbox Code Playgroud)
"makehtml被inithomefeed调用"打印到控制台,makeHomeHTML函数内的控制台日志也是如此.但是,get函数中没有显示任何控制台日志.我怎么能这样做它不会跳过get函数?
我有搜索具有特定类的每个元素的函数:
$("#stepSurveyCtnId .questionCtnClass").each(function () {}
Run Code Online (Sandbox Code Playgroud)
在每个步骤中,我检查一个问题是否是客户类型:
var type = $(this).children().data("question-type");
var isCustomerQuestion = false;
switch (type) {
case "Name":
case "Email":
isCustomerQuestion = true;
break;
}
Run Code Online (Sandbox Code Playgroud)
如果是客户类型,我从数据库中获取客户表的下一个ID:
if(isCustomerQuestion) {
if (customerId == -1) {
$.ajax({
method: "POST",
url: urlCustomerCreate,
success: function (ajaxData) {
customerId = ajaxData.NumericValue;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
问题是在.each()函数的第二次迭代中,customerId仍然是= -1,例如它应该是1305.
似乎执行不会在$ .ajax调用中停止,或者迭代在同一时间执行,第二次迭代不会从第一次迭代中接收customerId.
我目前正在使用jquery插件来读取数据文件(data.html)
data.html具有以下格式
[10,20,30,40,50]
Run Code Online (Sandbox Code Playgroud)
我的jquery数据请求和返回值的javascript如下
function test(){
var result=$.ajax({
url:'data.html',
type:'get',
dataType:'text',
async:false,
cache:false
}).responseText
return result;};
var my=test();
alert(my[0])
Run Code Online (Sandbox Code Playgroud)
我想以数组格式获取这些值,即我希望我的[0]值为10,但我得到"[".如果我使用eval功能
my=eval(test());
Run Code Online (Sandbox Code Playgroud)
我可以得到10,但有没有其他更好的方法将返回的ajax调用存储到数组而不是字符串?
谢谢
我尝试了下面的答案,我有点困惑,myArray中的跟随代码结果为null(在firebug中),但我把async:false然后它的工作原理.为什么我需要async:false来将值存储到数组中?(http://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-req)
jQuery.extend({getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'json',
cache: false,
success: function(data) {result = data;}
});
return result;}});
myArray=$.getValues("data.html");
alert(myArray[1]);
Run Code Online (Sandbox Code Playgroud) 我需要找出如何在post函数之外访问"data"变量.这将返回valid
或invalid
这样我就可以完成主要功能的逻辑.
这是正确的方法:
$('#form_choose_methods').submit(function(){
var voucher_code = $('#voucher_code').val();
var check = $.post(baseURL+"ajax.php", { tool: "vouchers", action: "check_voucher", voucher_code: voucher_code },
function(data) {
});
alert(check);
return false;
});
Run Code Online (Sandbox Code Playgroud)
check
似乎是对象,但我想知道如何访问它的结果.
我需要实现同步Ajax调用机制。我已经在我的助手中实现了 ajax 调用功能,如下所示:
MH.helper = {
ajax : function (option) {
if(option !== undefined) {
for(var opt in option) {
this[opt] = option[opt];
}
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
this.xhr=new XMLHttpRequest();
} else {
// code for IE6, IE5
this.xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还实现了 Ajax 原型,如下所示:
MH.helper.ajax.prototype = {
// XMLHttpRequest obj
xhr : null,
// request url
url: '',
// post funciton
post: function() {
var xhr = this.xhr;
var that …
Run Code Online (Sandbox Code Playgroud)