jQuery,AJAX,JSONP:即使它是空的,如何实际发送数组? [英] jQuery, AJAX, JSONP: how to actually send an array even if it's empty?

查看:244
本文介绍了jQuery,AJAX,JSONP:即使它是空的,如何实际发送数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读过这些问题,但没有一个能满足我的需求:

I've already read those questions but none of them answer to my need:

  • Testing for an empty array object in JSON with jQuery
  • jQuery 1.4.4+ AJAX request - post empty array or object becomes string
  • Cannot access data from jQuery Ajax request, returns empty array
  • JQuery removes empty arrays when sending

(最新的一个说只是添加硬编码的引号,即 [''] 但是我不能这样做,我正在调用一个返回的函数一个n数组)

(the latest one said just add hard-coded quotes ie [''] but I can't do this, I'm calling a function that returns an Array)

所以这是我的代码(注意问题在于空数组 new Array() ):

So here's my code (note that the problem lies to the empty array new Array()):

function AjaxSend() {
  $.ajax({
    url: '/json/myurl/',
    type: 'POST',
    dataType: 'jsonp',
    data : { 'tab':new Array() },
    context: this,
    success: function (data) {
      if (data.success) {
        console.log('ok');
      }   
      else {
        console.log('error');
      }   
    }   
  }); 
}

简单呃?
这是我的Php代码:

Simple eh? Here's my Php code:

echo '_POST='.var_export($_POST,true)."\n";

这是结果:

_POST=array (
)
jQuery1710713708313414827_1329923973282(...)

如果我将空数组更改为非空,即:

If I change the empty Array by a non-empty, i.e.:

'tab':new Array({ 't':'u' },{ 'v':'w' })

结果是:

_POST=array (
  'tab' => 
  array (
    0 => 
    array (
      't' => 'u',
    ),
    1 => 
    array (
      'v' => 'w',
    ),
  ),
)
jQuery1710640656704781577_1329923761425(...)

所以这显然意味着当有一个空的Array()要发送时,它会被忽略,并且它没有被添加到POST变量

So this clearly means that when there's an empty Array() to be sent, it is ignored, and it's not added to the POST variables.

我错过了什么?

PS:我的jQuery版本来自最新的谷歌CDN,即:

PS: my jQuery version is from the latest google CDN i.e.:

<啊ref =http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js =noreferrer> http://ajax.googleapis.com/ajax/libs/jquery/1 /jquery.min.js

http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui。 min.js

我希望发送数组,即使它是空的(=发送 [] )!
任何解决方案?任何的想法?我已经尝试添加此选项 traditional:true 但没有成功。

I want the array to be sent, even if it's empty (= send [])! Any solution? Any idea? I've already tried to add this option traditional: true without success.

推荐答案

问题是你无法真正发送空数组。您是否尝试手动发送空数组?那个uri看起来怎么样(注意它与POST的推理相同)?

The problem is that you can't really send empty array. Have you tried to send an empty array manually? How would that uri look (note that it's the same reasoning for POST)?

/path?arr[]

这会产生这样的$ _GET:

This would result in a $_GET like this:

array (
 'arr' => array (
    0 => ''
  )
)

这不是一个空数组,是吗?它是一个包含空字符串的单个元素的数组。那么jQuery做什么,我会同意这是处理它的正确方法,就是不要发送任何东西。

That's not really an empty array, is it? It's an array with a single element of an empty string. So what jQuery does, and I would agree that this is the correct way of handling it, is to not send anything at all.

这对你来说实际上非常简单检查服务器。只需添加一个额外的检查参数是否存在,即:

This is actually really simple for you to check on the server. Just add an extra check whether the parameter exists or not, i.e:

$tabs = array();
if(isset($_POST['tab'])) {
  $tabs = $_POST['tab'];
}

这篇关于jQuery,AJAX,JSONP:即使它是空的,如何实际发送数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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