普通网友 2015-11-30 10:24
浏览 110
已采纳

通过ajax发送多个数据

my question is an annoying combination of multiple smaller questions on stackoverflow as far as i've been able to find, but i can't get it figured out.

I'm trying to send an audio file over ajax, that works fine, but i also want to include the location where it was uploaded/ recorded. This is what I have made up reading through multiple similar question:

(it does not work)

function getLocation() {
    navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
    function foundLocation(position) {
        var longitude = position.coords.longitude;
        var latitude = position.coords.latitude;
        //alert("LAT : "+latitude +", LONG : "+longitude);
        return [longitude, latitude];
    }
}

function noLocation() {
    //alert('Could not find location');
}

function uploadAudio(mp3Data){
    var pos = getLocation();
    var longitude = pos[0];
    var latitude = pos[1];
    var reader = new FileReader();
    reader.onload = function(event){
        var fd = new FormData();
        var mp3Name = encodeURIComponent('audio_recording_' + new Date().getTime() + '.mp3');
        console.log("mp3name = " + mp3Name);
        fd.append('fname', mp3Name);
        fd.append('data', event.target.result);
        fd.append('longitude', longitude);
        fd.append('latitude', latitude);
        $.ajax({
            type: 'POST',
            url: 'upload.php',
            data: fd,
            processData: false,
            contentType: false
        }).done(function(data) {
            //console.log(data);
            log.innerHTML += "
" + data;
        });
    };
    reader.readAsDataURL(mp3Data);
}

The php script I have to handle the data is as follows:

<?php

if(!is_dir("recordings")){
    $res = mkdir("recordings",0777); 
}

// pull the raw binary data from the POST array
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1);
// decode it
$decodedData = base64_decode($data);
// print out the raw data, 
//echo ($decodedData);
$filename = 'audio_recording_' . date( 'Y-m-d-H-i-s' ) .'.mp3';
// write the data out to the file
$fp = fopen('recordings/'.$filename, 'wb');
fwrite($fp, $decodedData);
fclose($fp);
?>

I want to be able to retrieve the location with something like $latitude = $_POST['longditude'];

Right now I have no idea how to make JS return multiple data in the getLocation() function, nor how to make ajax send all this data properly. Any help will be greatly appreciated!

  • 写回答

2条回答 默认 最新

  • donglu2523 2015-11-30 10:31
    关注

    As you are sending file in raw format via AJAX, so you can not send more data in message body. So you should send data in GET (query string).

    Client Side:

    function uploadAudio(mp3Data){
        var pos = getLocation();
        var longitude = pos[0];
        var latitude = pos[1];
        var reader = new FileReader();
        reader.onload = function(event){
            var fd = new FormData();
            var mp3Name = encodeURIComponent('audio_recording_' + new Date().getTime() + '.mp3');
            console.log("mp3name = " + mp3Name);
            fd.append('fname', mp3Name);
            fd.append('data', event.target.result);
    
            var ajax_url = 'upload.php?longitude='+longitude+'&latitude='+latitude;
    
            $.ajax({
                type: 'POST',
                url: ajax_url,
                data: fd,
                processData: false,
                contentType: false
            }).done(function(data) {
                //console.log(data);
                log.innerHTML += "
    " + data;
            });
        };
        reader.readAsDataURL(mp3Data);
    }
    

    Server Side:

    $longitude = $_GET['longitude'];
    $latitude = $_GET['latitude'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?