- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我设置了一个按钮,单击该按钮时将通过获取发布数据并创建一个警报。但是,我首先检查以确保某些输入不为空。如果输入元素没有值,我会尝试返回一个字符串,告诉我哪个输入元素值为空。当我这样做时,会出现“Uncaught TypeError: postData(...).then is not a function”错误。我也看过Uncaught TypeError: $.get(...).then is not a function但无法弄清楚。任何帮助将不胜感激。谢谢你!
document.getElementById("sbmForm").onclick = function () {postData(`/projectInfo`)
.then(result => alert(JSON.stringify(result))) // JSON-string from `response.json()` call
.catch(error => alert(error))};
function postData(url = ``) {
// get form data from local storage
var data = {};
var unqVal = document.getElementById("unq_num").innerHTML;
unqVal = "--??**" + unqVal;
// keys that can not be empty
var notEmpty = ["comp_name", "compaddr_str_num", "compaddr_str_nam", "compaddr_city",
"compaddr_state", "compaddr_zipcode", "proj_name", "projaddr_str_num", "projaddr_str_nam",
"projaddr_city", "projaddr_state", "projaddr_zipcode", "contact_fname", "contact_date"];
/* get all keys and values from local storage and store them in dict to send to server */
var keys = Object.keys(localStorage);
var i;
for (i=0; i < keys.length; i++){
var val = localStorage.getItem(keys[i]);
// if unq val is in string, add it to the data
var indexOf = keys[i].indexOf(unqVal);
if (indexOf != -1){
var keyNU = keys[i].substring(0, indexOf);
// if value is in array
if (notEmpty.includes(keyNU)){
// if value is empty
if (!val){
alert(keyNU + " cannot be empty!");
/**** ERROR OCCURS HERE *****/
return keyNU + " cannot be empty!";
}
}
data[keys[i]] = val;
}
}
// Default options are marked with *
return fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(response => response.json()); // parses response to JSON
};
最佳答案
您返回的是字符串值,而不是 promise当检测到“错误”时。 then
方法只存在于 promises 上。
将您的函数
更改为async
所以它默认返回一个 promise
。
async function postData(url = ``){}
然后在需要发生的时候抛出一个错误(会被catch
方法捕获)通过改变这个
/**** ERROR OCCURS HERE *****/
return keyNU + " cannot be empty!";
到
throw new Error(keyNU + " cannot be empty!");
最后,对于您的提取:
//fetch returns a promise, so use await
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data), // body data type must match "Content-Type" header
});
//res.json() returns a promise, so use await
return await res.json();
关于javascript - 未捕获的 TypeError : postData(. ..).then 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54117909/
我正在从 Webview 发布到 https 服务器,如下面的 URL 所示,字符集为 BASE64。 Send data to page loaded in WebView 我的 postdata
我是 Android 编程新手。我正在尝试使用 post 将一些数据发布到服务器。我用谷歌搜索并想出了这个: public void postData() { // Create a new
我一直在使用 postData 动态设置请求参数。单击某个外部按钮后,我会更改网格 URL,并在 postData 中设置一些附加参数,如下所示。 JqGrid 似乎附加了所有这些参数及其所有后续
(免费)jqGrid 是否提供回调函数,我可以在加载网格之前设置 postData? 在我的例子中,它是伪postData,因为我将 jqGrid 与 loadOnce : true 一起使用。我正在
所以我有一个网格,我设置了一些默认值,就像这样...... defaults: { datatype: "json", mtype: "POST", autowidth: tr
这周我刚刚开始使用 jqGrid。我正在尝试创建一个通用函数,我可以调用它来加载具有不同 postData 参数(在函数调用中定义)的 jqGrid。 有什么办法可以做到这一点吗?我考虑的是创建一个数
事情是这样的, 为了模仿grails对一个表单进行多次提交的方式,我实现了这个拦截器,这样当它遇到如下网址时: /whatever-url/${controllerName}/index 它重定向到/
我的网络应用程序在本地 tomcat 服务器测试中运行良好。但是当部署到测试环境时,应用程序无法正常工作。 于是我远程debug了app,发现tomcat请求的postData为null,而本地deb
"wget --user-agent=Mozilla/5.0 --save-cookies cookies.txt --post-data 'dir="$dir1"=&file="$file1"' -
我想直接从博彩公司的网页上抓取他们的赌注。目前,我尝试从名为 unibet.com 的提供商处获取报价。问题:我需要发送一个发布请求,以便对我想要的报价进行适当的过滤。 因此我转到以下网页https:
嗨,我有一个带有子网格的 jqgrid,它调用 servlet。我使用 POST 向 servlet 发送一些数据,但在调用子网格时不会发送相同的数据。这是我的 JS: $("#testsTable"
我有一个 .asmx 网络服务,除了不读取 prostdata 之外,它工作正常。它看起来像这样: namespace mynamespace.liby { [ScriptService]
我的应用程序使用 Web 服务进行身份验证。正确的身份验证后,用户将被重定向到仪表板。 当我们设计应用程序时,我们强烈同意除非用户经过身份验证,否则绝不发送任何数据。 但是,当我通过身份验证算法进行调
我设置了一个按钮,单击该按钮时将通过获取发布数据并创建一个警报。但是,我首先检查以确保某些输入不为空。如果输入元素没有值,我会尝试返回一个字符串,告诉我哪个输入元素值为空。当我这样做时,会出现“Unc
我正在遵循 mightygio ( http://mightygio.com/2013/05/integrating-rails-and-angularjs-part-3-rest-and-angul
在the Node.JS docs for HTTP (v0.8.20),在 ServerRequest 下,我看到了 data 和 end 的事件。我没有看到 error 的事件。 通常,如果在接收
我正在尝试在 Sinatra 中创建一个页面,这样无论您发布什么(在参数名称“command”下)都会回显给您。这是我目前的做法: post '/eval' do "I got #{params[
我在 Visual Studio 2008 中有一个使用 .NET 3.5 的 Windows 窗体,上面有一个 WebBrowser 控件。在发送请求之前,我需要在 Navigating 事件处理程
我有一个简单的jqgrid @(Html.Grid("something") .SetCaption("") .AddColumn(new Column("ID").SetHidden(tru
似乎找不到任何与 gzip 压缩字符串相关的内容。只查找损坏的站点或不能作为 gzip 工作的压缩建议。还有很多关于服务器端实现的讨论。但是我希望从客户端发送编码数据。 澄清一下,我所有的客户都使用
我是一名优秀的程序员,十分优秀!