gpt4 book ai didi

javascript - 如何在没有 Post 方法或任何形式的情况下将信息作为 Json 数据发送到服务器

转载 作者:行者123 更新时间:2023-12-02 16:36:55 25 4
gpt4 key购买 nike

我的任务是使用 Json 将数据发送到服务器,而不实际使用“表单”或“Post”,以下是我和我的前辈之间的对话:

高级:

send only the data to the server without submitting the form in javascript

 myJsonObject.Username = document.getElementById('txtUsername').value;

this will be done when Save button click in such situation, we don't use form element we don't use submit button just a simple button for save

 onclick="saveUser()"
function saveUser(){
var userInfoJson = { };
userInfoJson.Username = document.getElementById('txtUsername').value;

...and so on just play around with json

我:

<form name="form" method="post" onsubmit="saveUser()">

will it look like this?

高级:

** don't use form tag don't use submit button

:
one more thing will it have <input type="text"> ?

高级:

not sure what u mean
u can use any kind of html, i don't mind
you need to show me some coding of json and that happens in .JS file
or in <script type="text\javascript" tag
html can be used to integrate ur user input
i don't mind u use any kind of html whether beautiful or not
I MEAN JSON here in this thread
I wan't json... json, json, json
don't bring the whole world here... talk about json
json only

那么如何在没有形式的情况下做到这一点呢?

最佳答案

我不明白不使用表单或提交按钮的原因,这似乎违反直觉,并且可能不是有效的标记。

但是要使其工作,您只需绑定(bind)到普通按钮即可...

http://jsfiddle.net/Labdr6pw/

<input type="text" id="txtUsername" name="username" />
<button id="submitButton" type="button">Save</button>
var submitButton = document.getElementById('submitButton'),
usernameInput = document.getElementById('txtUsername'),
userInfoJson = {};

userInfoJson.Username = usernameInput.value;

submitButton.addEventListener('click', function(){
var request = new XMLHttpRequest();

request.open("POST", "webservice", true);
request.onreadystatechange = function() {
if (request.readyState != 4 || request.status != 200) return;
console.log(request.responseText);
};
request.send(JSON.stringify(userInfoJson));
});

关于javascript - 如何在没有 Post 方法或任何形式的情况下将信息作为 Json 数据发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27858047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com