AJAX POST将数字作为字符串发送 [英] AJAX POST sends numbers as string

查看:600
本文介绍了AJAX POST将数字作为字符串发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AJAX POST请求因为某些原因将我的数字数据发送到我的服务器 string 以下是我的数据和AJAX请求:

I my AJAX POST request is sending my numeric data to my server as string for some reason... Here are my data and the AJAX request:

var data = {
        projectId: $("#projectId").val(),
        startDate: $("#startDate").val(),
        endDate: $("#endDate").val(),
        num_auto_tests: Number($("#num_auto_tests").val()),
        num_manual_tests: Number($("#num_manual_tests").val()),
        num_passed_tests: Number($("#num_passed_tests").val()),
        num_failed_tests: Number($("#num_failed_tests").val()),
        num_unran_tests: Number($("#num_unran_tests").val()),
        test: 3
    };

AJAX查询:

$.ajax({
        type: "POST",
        dataType: "json",
        url: "/addreport/+ data.projectId",
        data: data,
        success: function() {
            console.log('success');
        }
    });

console.log(typeof(data.num_auto_tests)); //returns `number`

服务器端返回:

{ projectId: 'FDIC-88445',
  startDate: '',
  endDate: '',
  num_auto_tests: '3',
  num_manual_tests: '3',
  num_passed_tests: '3',
  num_failed_tests: '3',
  num_unran_tests: '3',
  test: '3' } 

如您所见,应该是数字的值是服务器端的所有字符串。 。

As you can see, the values that should be numeric are all strings on the server side...

有谁知道发生了什么事?

Does anyone know what's going on?

提前致谢!

推荐答案

您的服务器收到HTTP协议中的帖子,难怪您的服务器端收到一个字符串,因为您正在执行的操作不是类型安全的。这实际上是预期的行为,如果您希望元素变为数字,然后将参数转换为数字,确切的方法取决于您使用的服务器端语言/框架。

Your server receives the post in HTTP protocol, it is no wonder your server-side receives a string, as the operation you are executing is not type-secure. This is actually the expected behavior and if you want the elements to become numeric, then convert the parameters to numbers, the exact methodology depends on the server-side language/framework you are using.

编辑:
你可以做两件事来解决你的问题:

You can do two things to solve your issue:


  1. 你可以创建一个数字处理程序/转换器,如下所示:

  1. You can create a numeric handler/converter, like this:

function detectNumeric(obj){
for(var ob index in obj){
if(!isNaN(obj [index])){
obj [index] = Number(obj [index]);
} else if(typeof obj ===object){
detectNumeric(obj [index]);
}
}
}

和以这种方式为你想要处理的任何对象调用此函数,或者

and call this function for any object you want to handle in such a way, or


  1. 将参数作为JSON传递并解码在服务器上。

var my_object = {

  position: 1,
  id: "500",
  text: "hello world",
  greeting: "100, good day to you",
  book: "nineteen eighty four"

};


// iterates over an object's properties 
// and converts numbers as strings to numbers
function detectNumeric(obj) {
  for (var index in obj) {
    // if object property value *is* a number, like 1 or "500"
    if (!isNaN(obj[index])) {
      // convert it to 1 or 500
      obj[index] = Number(obj[index]);
    }
    // to do: explain what this does
    // else if (typeof obj === "object") {
    //  detectNumeric(obj[index]);
    // }
  }

  console.log(my_object)
}

// call function
detectNumeric(my_object);

这篇关于AJAX POST将数字作为字符串发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆