微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何html形式post – 文件上传和从php服务器读取json响应

我正在尝试将文件上传PHP我的server.file和数据上传通过多部分/表格数据,在PHP服务器上收到的文件和数据但在我的PHP服务器上返回json响应.请帮助我如何在我的网页中读取json响应如果它的成功(代码= 0)意味着它重定向一个页面.phroid服务器对于android和网页都是常见的.json响应看起来像{“code”:0,“message”:“success”}

PHP echo $_SESSION["userid"]?>"> browseButton" onclick="" style="width:12%;height: 30px; text-decoration:none;">decoration:none;">browseButton.onclick=function chooseFile() { document.getElementById("file").click(); }; function showMyImage(fileInput) { var files = fileInput.files; var file = files[0]; var imageType = /image.*/; var img=document.getElementById("imgscreen"); var reader = new FileReader(); reader.onload = (function(aimg) { return function(e) { //x=e.target.result img.src = e.target.result; var extfilename=file.name; document.getElementById("filename").innerHTML=extfilename.slice(0,-5) ; document.getElementById("inputfilename").value=extfilename.slice(0,-5); }; })(img); reader.readAsDataURL(file); }
最佳答案
我认为它应该适合你.像我一样使用AJAX

     //Your PHP code
        $arrToJSON = array(
        "dataPHPtoJs"=>"yourData","asYouWant"=>"
PHP evaluate var dt={ ObjEvn:"btn_Login",dataJsToPHP: $("#txt_EmailLogin").val() }; //Ajax var request =$.ajax({//http://api.jquery.com/jQuery.ajax/ url: "yourServer.PHP",type: "POST",data: dt,dataType: "json" }); //Ajax Done catch JSON from PHP request.done(function(dataset){ for (var index in dataset){ dataPHPtoJsJS=dataset[index].dataPHPtoJs; asManyasYouWantJS=dataset[index].asYouWant; } //JavaScript conditions. Here you can control the behaivior of your html object,based on your PHP response if(dataPHPtoJsJS){ $( "#idYourHtmlElement" ).removeClass( "class1" ) $( "#idYourHtmlElement" ).addClass( "class2" ) } }); //Ajax Fail request.fail(function(jqXHR,textStatus) { alert("Request Failed: " + textStatus); }); }

原文地址:https://www.jb51.cc/html/426528.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐