簡體   English   中英

如何使用ajax將json數據發送到服務器

[英]how to send json data to server using ajax

refer.jvmhost.net/refer247/registration,這是我的網址,我必須獲取此網址的請求,如用戶詳細信息,並應以json格式獲取相應的響應,狀態為n錯誤,如果它包含..dont給我android代碼。 。

這是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>

這是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"]);
};

所以告訴我如何將json數據發送到該url n獲取一些響應,如果你已經存在電子郵件id已存在,如果你注冊了該id ..然后給出一些錯誤,如電子郵件ID已經存在n如果成功注冊然后給予respone就像注冊成功和狀態msg..200 okk ...

您可以使用ajax將json數據發布到指定的url / controller方法。 在下面的示例中,我發布了一個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);
     }
function addProductById(pId,pMqty){
            $.getJSON("addtocart?pid=" + pId + "&minqty="+ pMqty +"&rand=" + Math.floor((Math.random()*100)+1), function(json) {
                alert(json.msg);
            });
        }

這是一個簡單的例子,它將調用button clickonclick事件並調用addtocart servlet並傳遞2個參數,即pId and pMqty

成功完成后,它將在alert中返回消息,該消息在json中的servle中設置。

$.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");
    },
});
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']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM