Java 如何使用ajax将json数据发送到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20800763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send json data to server using ajax
提问by Seenu69
refer.jvmhost.net/refer247/registration, this is my url,i have to fetch request to this url like user details and should get the appropriate response in json
format with status n error if it contains ..dont give me android code..
refer.jvmhost.net/refer247/registration,这是我的 url,我必须像用户详细信息一样获取对该 url 的请求,并且json
如果它包含 ..
this is html
page.
这是html
页面。
<head>
<script type="text/javascript" src="json2.js"></script>
</head>
<body>
<div data-role="page" data-theme="c">
<div data-role="header" data-position="fixed" data-inset="true" class="paddingRitLft" data-theme="c">
<div data-role="content" data-inset="true"> <a href="index.html" data-direction="reverse"><img src="images/logo_hdpi.png"/></a>
</div>
</div>
<div data-role="content" data-theme="c">
<form name="form" method="post" onsubmit="return validate()">
<div class="logInner">
<div class="logM">Already have an account?</div>
<div class="grouped insert refb">
<div class="ref first">
<div class="input inputWrapper">
<input type="text" data-corners="false" class="inputrefer" placeholder="Userid" name="userid" id="userid" />
</div>
<div class="input inputWrapper">
<input type="password" data-corners="false" class="inputrefer" placeholder="Password" name="password" id="password" />
</div> <a href="dash.html" rel="external" style="text-decoration: none;"><input type="submit" data-inline="true" value="Submit" onclick="json2()"></a>
<p><a href="#" style="text-decoration: none;">Forgot Password</a>
</p>
</div>
</div>
<div class="logM">New user? Create refer Account</div>
<input type="button" class="btnsgreen" value="Sign Up! its FREE" class="inputrefer" data-corners="false" data-theme="c" />
</form>
</div>
</div>
<p style="text-align: center;">© refer247 2013</p>
</div>
</body>
this is json2.js
这是 json2.js
function json2()
{
var json1={"username":document.getElementById('userid').value,
"password":document.getElementById('password').value,
};
//var parsed = jsonString.evalJSON( true );
alert(json1["username"]);
alert(json1["password"]);
};
so tell me how to send the json data to that url n obtain some response like if email id is already exist if u registering with that id ..then give some error like email id already exist n if registerd succesfully then give respone like registerd successfully and status msg..200 okk...
所以告诉我如何将 json 数据发送到该 url n 获得一些响应,例如如果您使用该 id 注册电子邮件 ID 已经存在..然后给出一些错误,例如电子邮件 ID 已经存在 n 如果注册成功,然后给出类似注册成功的响应和状态消息..200 ok...
回答by monu
$.ajax({
url: urlToProcess,
type: httpMethod,
dataType: 'json',
data:json1,
success: function (data, status) {
var fn = window[successCallback];
fn(data, callbackArgs);
},
error: function (xhr, desc, err) {
alert("error");
},
});
回答by Java Curious ?
function addProductById(pId,pMqty){
$.getJSON("addtocart?pid=" + pId + "&minqty="+ pMqty +"&rand=" + Math.floor((Math.random()*100)+1), function(json) {
alert(json.msg);
});
}
Here is a simple example, which will call on button click
or onclick
event and call addtocart servlet
and passes 2 argument with it i.e. pId and pMqty
.
这是一个简单的示例,它将调用button click
或onclick
事件并调用addtocart servlet
并传递 2 个参数,即pId and pMqty
。
and after successful completion it return message in alert which is set in that servle
t in json
.
成功完成后,它在警报中返回消息,该消息在servle
t in 中设置json
。
回答by kasper Taeymans
var json1={"username":document.getElementById('userid').value,
"password":document.getElementById('password').value,
};
$.ajax({
url: '/path/to/file.php',
type: 'POST',
dataType: 'text',//no need for setting this to JSON if you don't receive a json response.
data: {param1: json1},
})
.done(function(response) {
console.log("success");
alert(response);
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
on the server you can receive you json and decode it like so:
在服务器上,您可以接收 json 并像这样对其进行解码:
$myjson=json_decode($_POST['param1']);
回答by Girish Sakhare
You can use ajax to post json data to specified url/controller method. In the below sample I am posting an json object. You can also pass each parameter separately.
您可以使用 ajax 将 json 数据发布到指定的 url/控制器方法。在下面的示例中,我发布了一个 json 对象。您还可以单独传递每个参数。
var objectData =
{
Username: document.getElementById('userid').value,
Password: document.getElementById('password').value
};
var objectDataString = JSON.stringify(objectData);
$.ajax({
type: "POST",
url: "your url with method that accpects the data",
dataType: "json",
data: {
o: objectDataString
},
success: function (data) {
alert('Success');
},
error: function () {
alert('Error');
}
});
And your method can have only one parameter of string type.
而且你的方法只能有一个字符串类型的参数。
[HttpPost]
public JsonResult YourMethod(string o)
{
var saveObject = Newtonsoft.Json.JsonConvert.DeserializeObject<DestinationClass>(o);
}