使用javascript函数将Json对象发送到服务器

时间:2023-01-21 21:18:48

I try to send a json object to a distant server, but I didnt receive success message. please what's wrong with this code:

我尝试将json对象发送到远程服务器,但我没有收到成功消息。请问这段代码有什么问题:

  function sendSMS(){
    var input = '{"header":"****","****":*****,"****":"*****"}';
    var url = "https://**********&username=*****&password=*******";

    jQuery.ajax({
        type: "POST",
        crossDomain:true,
        url: url,
        data: JSON.stringify(input),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        async: false,
        success: function(){
           alert("success");
        }
    });
    }
    // html code
    <html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
   <script type="text/javascript" src="http://www.json.org/json2.js"></script>
<script type="text/javascript" src="sendSMS.js"></script>
</head>

<body> 
<button onclick="sendSMS()">sendSMS</button>
</body>
</html>

Any help please.

请帮忙。

1 个解决方案

#1


0  

You have to simple change your ajax call to this:

你必须简单地将你的ajax调用更改为:

function sendSMS(){
var input = '{"header":"Ooredoo","msisdn":21620117297,"SMS":"Hello"}';
var url = "https://**********&username=*****&password=*******";

jQuery.ajax({
    type: "POST",
    crossDomain:true,
    url: url,
    data: JSON.parse(input),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    async: false,
    success: function(){
       alert("success");
    }
});
}

The main changes are JSON.stringify to JSON.parse this will help you to parse the JSON data into a JSON object, and the second change is the actual payload in which you missed a " at the end, just after Hello.

主要更改是JSON.stringify到JSON.parse,这将帮助您将JSON数据解析为JSON对象,第二个更改是您错过了“最后,就在Hello之后”的实际有效负载。

If you have any other question, just ask :)

如果您有任何其他问题,请问:)

Also I would recommend not to send the username and password as querystring parameter, use a POST request with a proper payload and last, if you can go through ssl

另外我建议不要将用户名和密码作为querystring参数发送,使用带有适当有效负载的POST请求,最后,如果你可以通过ssl

#1


0  

You have to simple change your ajax call to this:

你必须简单地将你的ajax调用更改为:

function sendSMS(){
var input = '{"header":"Ooredoo","msisdn":21620117297,"SMS":"Hello"}';
var url = "https://**********&username=*****&password=*******";

jQuery.ajax({
    type: "POST",
    crossDomain:true,
    url: url,
    data: JSON.parse(input),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    async: false,
    success: function(){
       alert("success");
    }
});
}

The main changes are JSON.stringify to JSON.parse this will help you to parse the JSON data into a JSON object, and the second change is the actual payload in which you missed a " at the end, just after Hello.

主要更改是JSON.stringify到JSON.parse,这将帮助您将JSON数据解析为JSON对象,第二个更改是您错过了“最后,就在Hello之后”的实际有效负载。

If you have any other question, just ask :)

如果您有任何其他问题,请问:)

Also I would recommend not to send the username and password as querystring parameter, use a POST request with a proper payload and last, if you can go through ssl

另外我建议不要将用户名和密码作为querystring参数发送,使用带有适当有效负载的POST请求,最后,如果你可以通过ssl