简体   繁体   English

Ajax发送数据Json类型错误

[英]Ajax send data Json type error

I want to submit Form ajax with jsontype 我想用jsontype提交Form ajax

Let say I have 5 Field in that text box out of 5, 4 textbox normal Text one field value itself json so that ajax throw the error. 假设我在该文本框中有5个字段,而在5个文本框中,普通文本的第一个字段值本身是json,所以ajax抛出错误。 How i can send 我如何发送

[![enter image description here][1]][1] [![在此处输入图片描述] [1]] [1]

my.js my.js

function _getTest(){
                var data = JSON.stringify($("#formId").serialize());
               //alert(data); 
                $.ajax({
                url: "domain.com",
                datatype: 'json',
                type: "POST",
                contentType: 'application/json',
                data: data,              
                    success: function(data) {
                        console.log(data);
                    },
                    error: function(result) {
                        alert(result.responseText);
                    }
                });
            }

After Submit form i got status this Status Code:400 Bad Request 提交表单后,我的状态为此状态代码:400错误的请求

General Tab Request URL: http://mycustomurl/com Request Method:POST Status Code:400 Bad Request Remote Address:35.154.113.130:8080 Referrer Policy:no-referrer-when-downgrade 常规选项卡请求URL: http:// mycustomurl / com请求方法:POST状态代码:400错误的请求远程地址:35.154.113.130:8080推荐人策略:no-referrer-when-downgrade

[![enter image description here][2]][2] [![在此处输入图片描述] [2]] [2]

Expected Payload 预期有效载荷

{
    "name": "user",
    "mobile": "1234567890",
    "email": "my@gmail.com",
    "address": "test",
    "localityID": 1,
    "cityID": 1,
    "bookingDate": "2017-12-20",
    "timingID": "1",
    "paymentType": 1,
    "affiliateID": 15,
    "key": "test",
    "password": "test",
    "orderItems": [{
        "id": 3,
        "quantity": 2
           }, {
        "id": 4,
        "quantity": 5
    }]
}

Based on my understanding I have created below logic to create your payload. 根据我的理解,我创建了以下逻辑来创建您的有效负载。 This code create exact structure you want to send as payload. 此代码创建您要作为有效内容发送的确切结构。 In my code I have created mainData array to create payload. 在我的代码中,我创建了mainData数组来创建有效负载。 In your case you need to pass this mainData as your ajax payload data. 在您的情况下,您需要将此mainData作为ajax有效负载数据传递。 Hopefully this will solve your problem 希望这可以解决您的问题

 var mainData={}; $(document).ready(function(){ $("#submit").click(function(){ mainData='{'+'\\n'+ '"name":'+ $("#name").val()+',\\n'+ '"mobile": '+$("#mobile").val()+',\\n'+ '"email": '+$("#email").val()+',\\n'+ '"address": '+$("#address").val()+',\\n'+ '"localityID": '+$("#localityID").val()+',\\n'+ '"cityID": '+$("#cityID").val()+',\\n'+ '"bookingDate":'+$("#bookingDate").val()+',\\n'+ '"timingID": '+$("#timingID").val()+',\\n'+ '"paymentType":'+$("#paymentType").val()+',\\n'+ '"affiliateID": '+$("#affiliateID").val()+',\\n'+ '"key": '+$("#key").val()+',\\n'+ '"password":'+$("#password").val()+',\\n'+ '"orderItems": '+$("#orderItems").val()+'\\n'+ ' });'; console.log("mainData="); console.log(mainData); }); }); 
 <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"> </script> <input type="text" id="name" placeholder="Name"/> <input type="number" id="mobile" placeholder="Mobile"/> <input type="text" id="email" placeholder="Email"/> <input type="text" id="address" placeholder="address"/> <input type="text" id="cityID" placeholder="City ID"/> <input type="text" id="localityID" placeholder="Locality ID"/> <input type="date" id="bookingDate" placeholder="Booking Date"/> <input type="text" id="timingID" placeholder="Timing ID"/> <input type="text" id="paymentType" placeholder="Payment Type"/> <input type="text" id="affiliateID" placeholder="Affiliate ID"/> <input type="text" id="key" placeholder="Key"/> <input type="password" id="password" placeholder="Password"/> <textarea type="text" id="orderItems" placeholder="order Items"></textarea> <input type="submit" id="submit" /> 

After created payload from above code you can pass payload in your ajax as follows 从上面的代码创建有效负载之后,您可以按如下所示在ajax中传递有效负载

function _getTest(){
                $.ajax({
                url: "domain.com",
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(mainData),              
                    success: function(data) {
                        console.log(data);
                    },
                    error: function(result) {
                        alert(result.responseText);
                    }
                });
            }

I am also attaching output screenshot for the same 我也附上相同的输出屏幕截图 在此处输入图片说明

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

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