不能传递null使用jQuery AJAX服务器。在服务器接收到的值是字符串"零" [英] Cannot pass null to server using jQuery AJAX. Value received at the server is the string "null"

查看:323
本文介绍了不能传递null使用jQuery AJAX服务器。在服务器接收到的值是字符串"零"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个JavaScript / PHP / Ajax应用程序使用jQuery,以确保与其他浏览器比Firefox的兼容性。

我有麻烦通过使用jQuery的Ajax功能的真,假和空值。

Javascript的code:

  $。阿贾克斯
(
   {
      网址:< SERVER_URL>中
      数据类型:JSON,
      键入:POST,
      成功:receiveAjaxMessage,
      数据:
      {
         valueTrue:真正的,
         valueFalse:假的,
         valueNull:空
      }
   }
);
 

PHP code:

 的var_dump($ _ POST);
 

服务器输出:

 阵列(3){
  [valueTrue] =>
  串(4)真
  [valueFalse] =>
  串(5)假
  [valueNull] =>
  串(4)空
}
 

的问题是空,真,假值正被转换为字符串。

JavaScript的AJAX $ C $当前使用的C会空,真,假正常,但只适用于Firefox浏览器。

有谁知道如何使用jQuery来解决这个问题呢?


下面是一些工作code(不使用jQuery)与上面给出的未工作code比较。

Javascript的code:

  ajaxPort.send
(
   &其中; SERVER_URL>中
   {
      valueTrue:真正的,
      valueFalse:假的,
      valueNull:空
   }
);
 

PHP code:

 的var_dump(json_de code(的file_get_contents(PHP://输入),真));
 

服务器输出:

 阵列(3){
  [valueTrue] =>
  布尔(真)
  [valueFalse] =>
  布尔(假)
  [valueNull] =>
  空值
}
 

请注意,空,真,假值正确接收。

还要注意的是,在第二个方法$ _POST数组没有在PHP code使用。我认为这是问题的关键,但我不能找到一种方法使用jQuery来复制这种行为。


这部分低于被接受的答案后添加。

下面是原来的code修正版本。

Javascript的code:

  $。阿贾克斯
(
   {
      网址:< SERVER_URL>中
      数据类型:JSON,
      键入:POST,
      成功:receiveAjaxMessage,
      数据:JSON.stringify
      (
         {
            valueTrue:真正的,
            valueFalse:假的,
            valueNull:空
         }
      )
   }
);
 

PHP code:

 的var_dump(json_de code(的file_get_contents(PHP://输入),真));
 

服务器输出:

 阵列(3){
  [valueTrue] =>
  布尔(真)
  [valueFalse] =>
  布尔(假)
  [valueNull] =>
  空值
}
 

解决方案

你会期待什么?您发送该值作为POST参数,这是简单的文本字符串。如果你想要的类型安全传输,使用某种类型的编码,如JSON。 (这是不是的dataType 做 - 这是指从服务器的响应)

I am converting a javascript/php/ajax application to use jQuery to ensure compatibility with browsers other than Firefox.

I am having trouble passing true, false, and null values using jQuery's ajax function.

Javascript code:

$.ajax
(
   {
      url     : <server_url>,
      dataType: 'json',
      type    : 'POST',
      success : receiveAjaxMessage,
      data:
      {
         valueTrue : true,
         valueFalse: false,
         valueNull : null
      }
   }
);

PHP code:

var_dump($_POST);

Server output:

array(3) {
  ["valueTrue"]=>
  string(4) "true"
  ["valueFalse"]=>
  string(5) "false"
  ["valueNull"]=>
  string(4) "null"
}

The problem is that the null, true, and false values are being converted to strings.

The Javascript AJAX code currently in use passes null, true, and false correctly but only works in Firefox.

Does anyone know how to solve this problem using jQuery?


Here is some working code (not using jQuery) to compare with the not-working code given above.

Javascript Code:

ajaxPort.send
(
   <server_url>,
   {
      valueTrue : true,
      valueFalse: false,
      valueNull : null
   }
);

PHP code:

var_dump(json_decode(file_get_contents('php://input'), true));

Server output:

array(3) {
  ["valueTrue"]=>
  bool(true)
  ["valueFalse"]=>
  bool(false)
  ["valueNull"]=>
  NULL
}

Note that the null, true, and false values are correctly received.

Note also that in the second method the $_POST array is not used in the PHP code. I think this is the key to the problem, but I cannot find a way to replicate this behavior using jQuery.


This section was added after the answer below was accepted.

Here is a corrected version of the original code.

Javascript code:

$.ajax
(
   {
      url     : <server_url>,
      dataType: 'json',
      type    : 'POST',
      success : receiveAjaxMessage,
      data    : JSON.stringify
      (
         {
            valueTrue : true,
            valueFalse: false,
            valueNull : null
         }
      )
   }
);

PHP code:

var_dump(json_decode(file_get_contents('php://input'), true));

Server output:

array(3) {
  ["valueTrue"]=>
  bool(true)
  ["valueFalse"]=>
  bool(false)
  ["valueNull"]=>
  NULL
}

解决方案

What would you expect? You are sending this values as POST parameters, which are simple text strings. If you want type-safe transfer, use some sort of encoding, such as JSON. (Which is not what dataType does - that refers to the response from the server.)

这篇关于不能传递null使用jQuery AJAX服务器。在服务器接收到的值是字符串&QUOT;零&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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