2013-05-26 165 views
0

如何将数据从javascript发送到php?如果我使用ajax,会出现错误? 这里是我的代码从javascript发送数据到php(phonegap android)

的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会如果IM这样的(指正错)

script.js with ajax

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,总会有误function.if我不是用ajax,我不能得到id_place

+0

您有一个SQL注入漏洞。 – SLaks

+0

**错误说**是什么? – SLaks

+0

这实际上并不是JSONP。 – SLaks

回答

1

随着JSONP,服务器不返回一个普通的JSON对象,而是返回一个在客户端上运行的JavaScript函数。然后,您的客户端代码执行从服务器返回的用于访问数据的功能。

我相信另一篇文章:Simple jQuery, PHP and JSONP example? 将解释如何在您的上下文中做到这一点。

为了更全面地理解JSONP的机制,可以使用http://en.wikipedia.org/wiki/JSONP