2016-11-30 63 views
1

我想用动态名称上传文件并获取这些动态名称。对JS变量的ajax请求的JSON响应

详细说来,我有2页form.phpupload.php。当按下上form.php然后请求发送到upload.php,其中2个文件(DLpath和PhotoIDPath)被上传到服务器以动态名称上传按钮,例如:

DLpath =文档/ 20161130232311i8hGn0HzJT276415832.png

而且

PhotoIDPath = documents/20161130232311SSRqCyIbKKalock.png

它工作正常。然后在upload.php,我编码的文件名作为JSON阵列

$response = array ('DLpath'=>$Dlpath ,'PhotoIDPath'=>$PhotoIDPath); 
echo json_encode($response); 

而且萤火虫快照:

enter image description here

我想DLpath在var jsDlpath和PhotoIDPath在var jsPhotoIDPath

而我的代码(不工作)得到的回应是:

complete: function(response) 
    { 
    var jsDlpath=response.DLpath; 
    var jsPhotoIDPath=response.PhotoIDPath; 
alert(jsDlpath+" - "+jsPhotoIDPath) 
} 

和报警显示:

undefined - undefine

如果你能帮助我在GWT JS变量这些价值观,我会很感激你的。

+0

你解析了'response'? ..尝试登录'response',如果它是一个字符串..使用'JSON.parse'解析它到'json对象' – noobHere

+1

你在'response'中接收了什么?你可以在这里发布吗? – 31piy

+1

我认为你应该在成功没有完成 – rad11

回答

1

既然你编码你在服务器端response你应该分析它在JS端,您可以使用$.parsejson()

success: function(response) 
{ 
    var response = $.parseJson(response); 
    //if $.parseJson dont work, use JSON.parse 

    var jsDlpath=response.DLpath; 
    var jsPhotoIDPath=response.PhotoIDPath; 

    alert(jsDlpath+" - "+jsPhotoIDPath) 
} 

注:使用success/done回调,而不是完整的。

希望这会有所帮助。

0

如果以纯javascript运行,您会发现有两个响应属性:responseText和responseXML。你可能想:

一个完整的例子,使用curl从https://gist.github.com/bitdivine/7ddd943387a4350336dd(但jQuery将做精为好)来获得开放性问题在Github上:

curl('https://api.github.com/repos/gchq/CyberChef/issues?state=open') 
.then((res) => JSON.parse(res.responseText)) 
.then((data) => console.log(data))