【问题标题】:How to set content type as application/json using Ajax call?如何使用 Ajax 调用将内容类型设置为 application/json?
【发布时间】:2019-03-01 11:47:21
【问题描述】:

有一个smartcontractGovtContract,在使用curl 命令时可以正常工作,

$curl -X POST -H "Content-Type: application/json" localhost:3000/govtcontract/set -d '{"value":"5"}' | jq

$curl -X GET -H "Content-Type: application/json" localhost:3000/govtcontract/get | jq

但是,当使用表单时,无法在请求标头中将内容类型设置为 JSON。

HTML 和 JS:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

function validateForm() {
    console.log("in validateForm");
    //var amt = document.forms["txForm"]["amtvalue"].value;
        var amt = document.getElementById("amtvalue").value;
        console.log(amt);
        if (isNaN(amt) || amt < 0) {
            alert("Amount is not valid");
            return false;
        }
        console.log("before ajax call");
        var sendValues = {
            value: amt
        }
         $.ajax({
             url: "/govtcontract/set",
             type: "POST",
             dataType:"json",
             data: JSON.stringify({
             value: amt
               }),
             contentType: "application/json",
             success: function(got) {
                  return console.log("shortened url: " + got);
                }
          });



    return true;
}
</script>
</head>
<body>
<h1>Enter the transaction details below:</h1>
<form name="txForm" action="http://127.0.0.1:3000/govtcontract/set" onsubmit="return validateForm()" method="post">
Enter the amount:<br>
<input type="number" name="amtvalue" id="amtvalue" value="0"/><br>

<input type="submit" value="Submit">
</form>
</body>
</html>

服务器app.js:

const bodyParser = require('body-parser');
const express = require('express');
const GovtContractInstance = new (require('./GovtContract.js'))();
const app = express();

// Uses json as the format for reading request bodies
app.use(bodyParser.json());

// Allow CORS policy to allow anyone to call these endpoints
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

// POST testing endpoint for echoing the body of post calls
// You can use this endpoint to ensure the format of your curl requests are correct
// ex: curl -X POST -H "Content-Type: application/json" localhost:3000/echo -d '{"copy": "cat"}'
app.post('/echo', (request, response) => {
    // Same as post call except used for /app endpoint for getting/initializing the backend data
    response.status(200).send(request.body);
});

// POST deploys the SimpleStorage.sol smart contract onto your network.
// ex: curl -X POST -H "Content-Type: application/json" localhost:3000/simplestorage/deploy
// Optionally, you can use a SimpleStorage contract that it already deployed by
// adding the deployed address to the end of the url
// ex. curl -X POST -H "Content-Type: application/json" localhost:3000/simplestorage/deploy/0xafcAfc6F48E23cEF78355A2f6D013310B84c6272
app.post('/govtcontract/deploy/:address?', (request, response) => {
    let address = request.params.address ? request.params.address : null;
    GovtContractInstance.deploy(address).then((deployedAddress) => {
        return response.status(200).send({contractAddress: deployedAddress});
    }).catch((error) => {
        console.log("Error in deploy: ", error);
        return response.status(400).send({errorMessage: JSON.stringify(error)});
    })
});

// POST Sets the value stored in the contact to the value set in the request body
// ex: curl -X POST -H "Content-Type: application/json"     localhost:3000/simplestorage/set -d '{"value": "5"}'
app.post('/govtcontract/set', (request, response) => {
    if (!request.body.value) {
        return response.status(400).send({errorMessage: "No value set in request body"});
    }
    GovtContractInstance.set(request.body.value).then((txReceipt) => {
        return response.status(200).send({receipt: txReceipt});
    }).catch((error) => {
        console.log(error);
        return response.status(400).send({errorMessage: JSON.stringify(error)});
    })
});

// GET Returns the value stored in the contract
// ex: curl -X GET -H "Content-Type: application/json" localhost:3000/simplestorage/get
app.get('/govtcontract/get', (request, response) => {
    GovtContractInstance.get().then((value) => {
    return response.status(200).send({storedValue: value});
    }).catch((error) => {
        console.log(error);
        return response.status(400).send({errorMessage: JSON.stringify(error)});
    })
});

// Listen on port 3000
app.listen(3000, () => {
    console.log('listening on port 3000');
});

如请求标头所示,表单以application/x-www-form-urlencoded 发送。无法弄清楚这一点。蒂亚!

【问题讨论】:

    标签: jquery html json node.js ajax


    【解决方案1】:

    欢迎来到 Stack Overflow。

    您的电话存在多个问题。您正在使用 FORM submit 和 AJAX call 也用于相同目的。仅使用其中之一。使用AJAX 调用,您将能够维护单页应用程序,所以我的解决方案将只专注于此。

    由于我们使用AJAX 调用,因此不需要表单和表单操作,因为这些将由AJAX url 处理。

    HTML 和 JS:

    <html>
        <head>
        </head>
        <body>
            <h1>Enter the transaction details below:</h1>
            Enter the amount:<br>
            <input type="number" name="amtvalue" id="amtvalue" value="0"/><br>
    
            <p onClick="validateForm();">Submit</p>
        </body>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
            function validateForm() {
                console.log("in validateForm");
    
                var amt = document.getElementById("amtvalue").value;
    
                console.log(amt);
                if (isNaN(amt) || amt < 0) {
                    alert("Amount is not valid");
                    return false;
                }
    
                console.log("before ajax call");
    
                $.ajax({
                    url: "/govtcontract/set",
                    method: "POST",
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: {
                        value: amt
                    },
                    success: function(got) {
                        console.log("shortened url: " + got);
                    }
                });
            }
        </script>
    </html>
    

    同样在服务器 app.js 上添加以下内容以获取 req.body 中的任何类型的数据

      // configure the app to use bodyParser() to extract body from request.
      // parse urlencoded types to JSON
      app.use(bodyParser.urlencoded({
      extended: true
      }));
    
      // parse various different custom JSON types as JSON
      app.use(bodyParser.json({ type: 'application/*+json' }));
    
      // parse some custom thing into a Buffer
      app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }));
    
      // parse an HTML body into a string
      app.use(bodyParser.text({ type: 'text/html' }));
    

    对于使用 NodeJS 和 HTML 的 Web 应用程序的初学者指南,请查看 this repository,这说明了单页应用程序以及多页和单页之间的区别。

    【讨论】:

    • 非常感谢!但是,这是在请求有效负载中以 value=1 的形式发送我的数据,而不是 json 格式,这会导致 400 Bad Request 错误。我该怎么做才能使它以正确的格式发送数据?
    • 很难知道您可能犯了什么错误,请您使用我建议的回购,因为它工作正常。
    【解决方案2】:

    您使用返回字符串的JSON.stringify()。 您只需在 data 属性中传递一个对象

    {
      ...
      data: { value : amt },
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 2021-01-04
      • 2023-03-06
      • 2017-04-27
      • 1970-01-01
      • 2017-05-06
      • 2017-07-06
      相关资源
      最近更新 更多