Javascript数组转换为jQuery.post';s数据
我正在尝试使用jQuery.post向php脚本发送不同的值 我想发送的数据是这样的链接Javascript数组转换为jQuery.post';s数据,javascript,php,jquery,arrays,Javascript,Php,Jquery,Arrays,我正在尝试使用jQuery.post向php脚本发送不同的值 我想发送的数据是这样的链接 http://example.com/image1.jpg http://example.com/image2.jpg http://example.com/image3.jpg http://example.com/image4.jpg var postData = { action : 'ajax-rform' }; for(var i = 0; i < fileLinks.length; i+
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
var postData = { action : 'ajax-rform' };
for(var i = 0; i < fileLinks.length; i++){
postData["link"+(i+1)] = fileLinks[i];
}
我没有这些链接的确切数量,它们会因与我的脚本交互的用户而异
我将这些链接存储在javascript数组“fileLinks”中
我想做的是使用jQuery.post将这些链接发送到php
我可以用这个形状格式化这些链接吗
jQuery.post(MyAjax.ajaxurl,
{ action : 'ajax-rform',
link1: http://example.com/image1.jpg,
link2: http://example.com/image2.jpg,
link3: http://example.com/image3.jpg,
link4: http://example.com/image4.jpg,
},
function(data){
jQuery("#fileupload").after('<h1>Success</h1><p>Your registration has been recieved.</p>');
}
);
jQuery.post(MyAjax.ajaxurl,
{操作:'ajax rform',
链接1:http://example.com/image1.jpg,
链接2:http://example.com/image2.jpg,
链接3:http://example.com/image3.jpg,
链接4:http://example.com/image4.jpg,
},
功能(数据){
jQuery(“文件上传”)。在('Success您的注册已收到。')之后;
}
);
在php中,我只需要使用for循环并用数组替换link1和,php会为我迭代它,但在javascript中我应该怎么做
谢谢为什么不创建一个json数组,然后使用php的
json\u decode
方法呢?也许你应该创建一个数据对象来发送,像这样从数组中填充它
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
var postData = { action : 'ajax-rform' };
for(var i = 0; i < fileLinks.length; i++){
postData["link"+(i+1)] = fileLinks[i];
}
var postData={action:'ajax rform'};
对于(var i=0;i
然后在ajax中使用它
jQuery.post(MyAjax.ajaxurl,
postData,
function(data){
jQuery("#fileupload").after('<h1>Success</h1><p>Your registration has been recieved.</p>');
}
);
jQuery.post(MyAjax.ajaxurl,
postData,
功能(数据){
jQuery(“文件上传”)。在('Success您的注册已收到。')之后;
}
);
只需通过阵列即可。jQuery将使用数组表示法对其进行编码,PHP将决定将其返回到一个数组中:
jQuery.post(MyAjax.ajaxurl,
{ action : 'ajax-rform',
link: fileLinks
},
function(data){
jQuery("#fileupload").after('<h1>Success</h1><p>Your registration has been recieved.</p>');
});
jQuery.post(MyAjax.ajaxurl,
{操作:'ajax rform',
链接:文件链接
},
功能(数据){
jQuery(“文件上传”)。在('Success您的注册已收到。')之后;
});
在PHP中,您可以通过
$\u POST['link']
访问该数组。当我使用它时,它会以同步连接的方式发送fileLinks值,并且由于该PHP脚本多次运行,我希望PHP只执行一次。有什么办法吗?如果真是这样,那是因为其他原因。此代码不会导致多个连接。谢谢Barmer-是的,我使用的是正确的。每个方法和它都会导致此问题。