2014-04-09 227 views
0

我正在使用phonegap應用程序。我想將數據映像發送到服務器,但我無法發送它。Ajax將圖像發送到服務器

function addSiteToServer() { 
    var cId = localStorage.getItem("cId"); 
    var sname = $('#sitename').val(); 
    var slat = $('#lat').val(); 
    var slng = $('#lng').val(); 
    var storedFieldId = JSON.parse(localStorage["field_id_arr"]); 
    var p = {}; 
    for (var i = 0; i < storedFieldId.length; i++) { 
     var each_field = storedFieldId[i]; 
     var val_each_field = $('#' + each_field).val(); 
     p[each_field] = val_each_field; 
     console.log("p" + p); 
    } 
    var online = navigator.onLine; 
    if (online) { 
     var data = { 
      site: { 
       collection_id: cId, 
       name: sname, 
       lat: slat, 
       lng: slng, 
       properties: p 
      } 
     }; 
     //function sending to server 
     $.ajax({ 
      url: App.URL_SITE + cId + "/sites?auth_token=" + storeToken(), 
      type: "POST", 
      data: data, 
      enctype: 'multipart/form-data', 
      crossDomain: true, 
      datatype: 'json', 
      cache: false, 
      contentType: false, 
      processData: false, 
      success: function(data) { 
       console.log("data: " + data); 
       alert("successfully."); 
      }, 
     } 

回答

1

看起來你正在使用的常規方法來發送數據/圖像服務,這也是不建議的PhoneGap /科爾多瓦框架。

我請你用下面的方法,它可以作爲你的預期,我還使用本地存儲功能,值發送到服務器代替你的代碼,

function sendDataToServer(imageURI) { 
    var options = new FileUploadOptions(); 
    options.fileKey="file"; 
    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
    options.mimeType="image/jpeg"; 

    var params = {}; 

    params.some_text = localStorage.getItem("some_text"); 
    params.some_id = localStorage.getItem("some_id"); 
    params.someother_id = localStorage.getItem("someother_id"); 
    options.params = params; 

    var ft = new FileTransfer(); 
    ft.upload(imageURI, encodeURI("http://example.co.uk/phonegap/receiveData.php"), win, fail, options); 
} 
function win(r) { 
    console.log("Code = " + r.responseCode+"Response = " + r.response+"Sent = " + r.bytesSent); 
} 

function fail(error) { 
    alert("An error has occurred: Code = " + error.code); 
} 
function saveData(){ 
    sendDataToServer(globalvariable.imageURI); 
    alert("Data Saved Successfully"); 
} 

希望這有助於。

+1

謝謝你的回答。但我不知道爲什麼FileUploadOptions沒有定義。我已經安裝了文件傳輸。 – SANSONAN

+0

我想你會錯過在XML文件傳輸線。只需檢查您的xml文件是否添加或不添加。此外,如果您對我的答案感到滿意,只需通過選擇刻度標記接受它。謝謝 – Vinith

相關問題