这是我的NodeJs API用于注册
API.post('/register',(req,res) => {Account.register(new Account({username: req.body.username}),req.body.password,function(err,account){ console.log("acc: "+account); if(err){ if (err.name == "UserExistsError") { console.log("User Exists"); return res.status(409).send(err); }else { console.log("User Error 500"); return res.status(500).send(err); } }else { let newUser = new User(); newUser.accountID = account._ID; newUser.name = req.body.fullname; newUser.gender = req.body.gender; newUser.role = req.body.role; newUser.country = req.body.country; newUser.coins = req.body.coins; newUser.save(err => { if(err){ console.log(err); return res.send(err); }else{ console.log('user saved'); res.Json({ message: 'User saved' }); } }); passport.authenticate( 'local',{ session: false })(req,res,() => { res.Json({ registermsg: 'Successfully created new account'}); }); }});});
这是我在Unity C中的POST协同程序#
IEnumerator Post(string b) { byte[] bytes = System.Text.EnCoding.ASCII.GetBytes(b); using (UnityWebRequest www = new UnityWebRequest(BASE_URL,UnityWebRequest.khttpVerbPOST)) { UploadHandlerRaw uH = new UploadHandlerRaw(bytes); www.uploadHandler = uH; www.SetRequestheader("Content-Type","application/Json"); yIEld return www.Send(); if (www.isError) { DeBUG.Log(www.error); } else { lbltext.text = "User Registered"; DeBUG.Log(www.ToString()); DeBUG.Log(www.downloadHandler.text); } }}
我正在尝试DeBUG.Log(www.downloadHandler.text);但是我得到了NullReferenceException.
我想问一下,我正在使用的方式在我的API中返回响应是否正确?如果是,我如何在Unity端使用该响应.
解决方法 UnityWebRequest.Post,UnityWebRequest.Get和其他创建UnityWebRequest新实例的UnityWebRequest functions将自动附加DownloadHandlerBuffer.现在,如果使用UnityWebRequest
构造函数创建UnityWebRequest的新实例,则DownloadHandler不会附加到该新实例.您必须使用DownloadHandlerBuffer
手动执行此 *** 作.
IEnumerator Post(string b){ byte[] bytes = System.Text.EnCoding.ASCII.GetBytes(b); using (UnityWebRequest www = new UnityWebRequest(BASE_URL,UnityWebRequest.khttpVerbPOST)) { UploadHandlerRaw uH = new UploadHandlerRaw(bytes); DownloadHandlerBuffer dH = new DownloadHandlerBuffer(); www.uploadHandler = uH; www.downloadHandler = dH; www.SetRequestheader("Content-Type","application/Json"); yIEld return www.Send(); if (www.isError) { DeBUG.Log(www.error); } else { lbltext.text = "User Registered"; DeBUG.Log(www.ToString()); DeBUG.Log(www.downloadHandler.text); } }}总结
以上是内存溢出为你收集整理的c# – UnityWebRequest.downloadHandler返回null,同时从Node Server获得响应全部内容,希望文章能够帮你解决c# – UnityWebRequest.downloadHandler返回null,同时从Node Server获得响应所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)