如何解决代理对象在为其分配值后将值设置为未定义
我正在向服务器发送 xmlhttp 请求。我想发送一个包含通过代理验证的对象的 POST 请求。但是,在为对象属性赋值后,它仍然显示为未定义。 在 POST 请求中发送对象之前,我必须对对象执行验证。对某些特定属性(例如:ProductId)执行验证。
使用代理发送 POST 请求后,遇到内部服务器错误(500)。如果代理被移除,则 POST 请求成功执行(200)。
const validateData = {
set: function(target,property,value)
{
if(property === 'ProductId')
{
if(typeof(value) != "string")
{
alert(`Value of ${property} ain't string type`);
return false;
}
}
if(property === 'BasePrice')
{
if(typeof(value) != "number")
{
alert(`Value of ${property} ain't number type`);
return false;
}
}
if(property === 'ProductName')
{
if(value.length>30)
{
alert(`Value of ${property} exceeded 30 characters`);
return false;
}
}
return true;
},get: function(target,property)
{
return target.property;
}
};
targetData = {};
let ProxyData = new Proxy(targetData,validateData)
ProxyData.BasePrice = 911420;
console.log(ProxyData.BasePrice);
ProxyData.CategoryName = 'Toothpaste';
ProxyData.Description = 'Toothpaste';
ProxyData.Manufacturer = 'Vicco';
ProxyData.ProductId = 'Prd100';
ProxyData.ProductName = 'Vajradanti';
targetData.BasePrice = ProxyData.BasePrice;
targetData.CategoryName = ProxyData.CategoryName;
targetData.Description = ProxyData.Description;
targetData.Manufacturer = ProxyData.Manufacturer;
targetData.ProductId = ProxyData.ProductId;
targetData.ProductName = ProxyData.ProductName;
xhr.open('POST','https://apiapptrainingnewapp.azurewebsites.net/api/Products');
xhr.setRequestHeader("Content-Type","application/json");
xhr.send(JSON.stringify(ProxyData));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。