如何将数据从javascript发送到php?如果我使用ajax,会有错误吗? 这是我的代码
的script.js
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageURI, "http://*my_ip*/TA/php/upload.php", win, fail, options);
};
upload.php的
include 'db.php';
$t=time();
$id_place = $_GET["id_place"];
$file_name = $_FILES["file"]["name"].$t.".jpg";
$dir_full = "images/full/"."full_".$file_name;
$dir_small = "images/small/"."small_".$file_name;
move_uploaded_file($_FILES["file"]["tmp_name"], $dir_full);
$im_src = imagecreatefromjpeg($dir_full);
$src_width = imageSX($im_src);
$src_height = imageSY($im_src);
$dst_width = 50;
$dst_height = ($dst_width/$src_width)*$src_height;
$im = imagecreatetruecolor($dst_width,$dst_height);
imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
imagejpeg($im, $dir_small);
$temp = "http://*my_ip*/TA/php/images/full/"."full_".$file_name;
$temp2 = "http://*my_ip*/TA/php/images/small/"."small_".$file_name;
//$id_place = .$id_place;
$query = "insert into gallery values ('','$id_place','$temp','$temp2')";
mysql_query($query);
imagedestroy($im_src);
imagedestroy($im);
如果我使用ajax,script.js将会是这样的(如果我错了,请纠正我)
带有ajax的script.js
function uploadPhoto(imageURI) {
jquery.ajax({
type: 'GET',
url: 'http://203.189.122.77/TA/php/gallery.php',
data: {id_place: window.localStorage("id_place")},
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
//alert(window.localStorage.getItem("id_place"));
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageURI, "http://203.189.122.77/TA/php/upload.php", win, fail, options, true);
},
error: function(){
alert('There was an error when download images');
}
});
};
如果我使用ajax,总会出现错误函数。如果我不使用ajax,我就无法获取id_place
答案 0 :(得分:1)
使用JSONP,服务器不返回常规JSON对象,而是返回要在客户端上运行的JavaScript函数。然后,您的客户端代码将执行从服务器返回的函数以访问数据。
我相信另一篇文章:Simple jQuery, PHP and JSONP example? 将在您的上下文中解释如何执行此操作。
为了更一般地理解JSONP的机制,可能会使用http://en.wikipedia.org/wiki/JSONP。