简体   繁体   English

检查Ajax请求是否格式正确

[英]Check Ajax Request is well formed or Not

I using a ajax request to send a json string to a java web service at the back end from an html form. 我使用ajax请求从html表单将json字符串发送到后端的java web服务。

HTML code .. HTML代码 ..

  <!DOCTYPE html>
  <html>
  <head>
  <meta charset="ISO-8859-1">
  <title>Requests form</title>
  <script type="text/javascript" src="../js/jquery-1.11.3.min.js"></script>
  <script type="text/javascript">
  var ItemId = null;
  var UserId = null;
  var Token = null;

    submit1 = function() {
    alert("Inside submit function");
    Token = document.getElementById("itemId").value;
    UserId = document.getElementById("userId").value;
    var req = {
            requestoruserId: UserId,
            token: Token,

    }
    send(req);
    };

    send = function(req) {
    alert("Inside send function.");
    alert(JSON.stringify(req));
    $.ajax({
        url: '/flsv2/GetRequestsByUser',
        type:'GET',
        data: JSON.stringify(req),
        contentType:"application/json",
        dataType: "json",

        success: function(response) {
            alert("working");
            document.getElementById("Id").value = response.token;
            document.getElementById("Code").value = response.Title;
            document.getElementById("Message").value = response.Desc;
            document.getElementById("itemId1").value = response.RequestId;
            document.getElementById("userId1").value = response.Owneruserid;
            document.getElementById("date").value = response.Date;

            /*if(parseInt(response.Code) === 27 || parseInt(response.Code) === 28) {
                obj = JSON.parse(response.Message);
                document.getElementById("itemId1").value = obj.itemId;
                document.getElementById("userId1").value = obj.userId;
                document.getElementById("Message").value = obj.date
            }
            else{
                document.getElementById("Message").value = response.Message
            }*/
            },

            error: function() {
            alert("not working");
            //alert(data);
             }
            });
            };

           </script>
           </head>
           <body>
           <h1>My Requests FORM</h1>
           <form>
           Request Token Id : <input type="text" name="itemId" id="itemId">  <br/><br/>
           Request User Id : <input type="text" name="userId" id="userId"><br/><br/>
           <input type="button" value="Submit" onclick="submit1()">
           <h1>OUTPUT</h1>
           token : <input type="text" name="Id" id="Id"><br/><br/>
            title : <input type="text" name="code" id="Code"><br/><br/>
            Description : <input type="text" name="message" id="Message"><br/><br/>
            Request Id : <input type="text" name="itemId1" id="itemId1"><br/><br/>
             Owner Id : <input type="text" name="userId1" id="userId1"><br/><br/>
             Request Date : <input type="text" name="date" id="date"><br/><br/>
             </form>
             <br/>
             <h3><A href="index.html">INDEX</A></h3>
             </body>
             </html>

But Every time i click the submit button I get the error alert saying "not working". 但是,每当我单击“提交”按钮时,我都会收到错误提示“不起作用”。 on the browser console it says the following.. 在浏览器控制台上显示以下内容。

安慰

I think the error is because the json request is not well formed. 我认为错误是因为json请求格式不正确。 Is there a way of checking whether get() method of json string is well formed or not?? 有没有一种方法可以检查json字符串的get()方法是否格式正确?

data: JSON.stringify(req)

Don't stringify it! 不要将其归类! jQuery's AJAX API is expecting an actual object here, not a JSON string. jQuery的AJAX API在这里需要一个实际的对象,而不是JSON字符串。 It takes the object and constructs the HTTP querystring from it. 它接受对象并从中构造HTTP查询字符串。 It cannot do that from the JSON-string-version of your data. 它无法通过数据的JSON-string-version做到这一点。

You can see from the error message that the HTTP request is not what you want it to be. 您可以从错误消息中看到HTTP请求不是您想要的。

This doesn't tell you how to check well-formedness of the AJAX request in general (checking for success is really the only way to do that, as server-side APIs can be anything), but it does tell you how to fix this particular problem: 这不会告诉您一般如何检查AJAX请求的格式(检查成功实际上是唯一的方法,因为服务器端API可以是任何东西),但是它确实告诉您如何解决此问题。特殊问题:

data: req

It's a shame the back-end service is giving you an "Internal Server Error": that indicates a bug, because that is not the correct error code for a malformed request. 后端服务为您提供“内部服务器错误”是很可惜的:它指示一个错误,因为那不是格式错误的请求的正确错误代码。

I think url you want is '/flsv2/GetRequestsByUser?requestoruserId=whateverId&token=whateverToken' 我认为您想要的网址是'/ flsv2 / GetRequestsByUser?requestoruserId = whateverId&token = whateverToken'

 $.ajax({ url: '/flsv2/GetRequestsByUser', type:'GET', //data: JSON.stringify(req), params: req, // instead of data... contentType:"application/json", dataType: "json" }); 

You need to send an object to the "data" parameter of the ajax call. 您需要将一个对象发送到ajax调用的“数据”参数。 Change the data parameter like the following: 更改数据参数,如下所示:

$.ajax({
        url: '/flsv2/GetRequestsByUser',
        type:'GET',
        //data: JSON.stringify(req),
        data: {"request": JSON.stringify(req)},
        contentType:"application/json",
        dataType: "json"
});

This will produce a query string parameter in your url like: 这将在您的网址中生成一个查询字符串参数,例如:

/flsv2/GetRequestsByUser?request=STRINGIFIED_JSON_OBJECT / flsv2 / GetRequestsByUser?request = STRINGIFIED_JSON_OBJECT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM