繁体   English   中英

请求响应不起作用

[英]Request response doesn't work

这是我的JSON函数:

function createjson(){
  var json='{"jsondata":[[';
  $("#kup td").each(function(){
  if($(this).html()!="")
  {
    json+= parseInt($(this).html())+",";
    if($(this).index()%5==0 && $(this).index()!=0){
      json=json.substring(0,json.length-1);
      json+="],["
    }                       
  }             
 });
json=json.substring(0,json.length-3);
json+="]]}";
console.log(json); //-> works fine.
return json;
};

AJAX部分:

$("#button").click(function(){
  $.ajax({
    type:"POST",
    url:"x.php",
    data: createjson(),
    contentType: "application/json",
dataType: "json",
    success: function(result) {
        alert("done"); //->works
    }
  });           
}); 

PHP部分:

<?php
   header('Content-type: application/json');
   echo "<pre>";
   echo    $_POST['jsondata'];
   echo "</pre>";
?>

因此,“警报”有效,但是在控制台中检查响应时,它仅返回“ <pre></pre>

有什么办法吗?

数据选项可以包含格式为key1 = value1&key2 = value2的查询字符串,也可以包含格式为{key1:'value1',key2:'value2'}的对象。 如果使用后一种形式,则在发送数据之前,将使用jQuery.param()将数据转换为查询字符串。 可以通过将processData设置为false来绕过此处理

jQuery API文档

我没有对其进行测试,但是应该强烈尝试更改结构,并尝试一下:

function createdata(){
  var data;
  $("#kup td").each(function(){
    if($(this).html()!="")
    {
      data.push( parseInt( $(this).html() ) );
      //...                    
    }             
  });
console.log(data);
return data;
};

$("#button").click(function(){
  var data = createdata();
  $.ajax({
    type:"POST",
    url:"x.php",
    data: {createjson:data},
    contentType: "application/json",
    dataType: "json",
    success: function(result) {
        alert("done"); //->works
    }
  });           
}); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM