Java 如何使用ajax向服务器发送json数据

Java 如何使用ajax向服务器发送json数据,java,javascript,jquery,ajax,Java,Javascript,Jquery,Ajax,referel.jvmhost.net/refere247/registration,这是我的url,我必须像获取用户详细信息一样获取此url的请求,并且应该以json格式获得相应的响应,如果包含..不要给我安卓代码 这是html页面 <head> <script type="text/javascript" src="json2.js"></script> </head> <body> <div data

referel.jvmhost.net/refere247/registration,这是我的url,我必须像获取用户详细信息一样获取此url的请求,并且应该以
json
格式获得相应的响应,如果包含..不要给我安卓代码

这是
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;">&#169; refer247 2013</p>
    </div>
</body>
因此,告诉我如何将json数据发送到该url,并获得一些响应,如if-email 如果您使用该id注册,则id已存在。然后给出一些错误 类似电子邮件id已存在n如果注册成功,则给出类似注册成功的响应和状态消息..200 OK

$.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");
    },
});
下面是一个简单的示例,它将调用
按钮单击
onclick
事件并调用
addtocart servlet
,并传递2个参数,即
pId和pMqty

成功完成后,它返回警报中的消息,警报设置在
json
中的
servle
t中

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");
});
在服务器上,您可以接收json并按如下方式解码:

$myjson=json_decode($_POST['param1']);

您可以使用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');
            }
        });
并且您的方法只能有一个字符串类型的参数

     [HttpPost]
    public JsonResult YourMethod(string o)
    {
      var saveObject = Newtonsoft.Json.JsonConvert.DeserializeObject<DestinationClass>(o);
     }
[HttpPost]
公共JsonResult方法(字符串o)
{
var saveObject=Newtonsoft.Json.JsonConvert.DeserializeObject(o);
}

$.ajax({url:refere.jvmhost.net/refere247/registration],类型:'POST',内容类型:'application/json',数据:json.stringify(json1),数据类型:'json'});成功:函数(json1){console.log(“成功:+json1.userid”);},错误:函数(json1){console.log(“错误:+json1”);}; };这是ajax代码..可能包含一些错误..但我没有得到..因此请找到n correct mether没有必要将数据类型设置为json,除非服务器使用json响应。阅读问题:“应该以json格式获得适当的响应”。同样来自注释:“contentType:'application/json',因此服务器也需要json。为什么您认为OP在服务器上使用PHP?
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');
            }
        });
     [HttpPost]
    public JsonResult YourMethod(string o)
    {
      var saveObject = Newtonsoft.Json.JsonConvert.DeserializeObject<DestinationClass>(o);
     }