发布JSON字符串到PHP页面 [英] Posting JSON string to PHP page

查看:206
本文介绍了发布JSON字符串到PHP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有张贴JSON字符串到PHP页面的一些自杀的问题。我已经经历了前十名的结果与我的问题,所以问题对谷歌和大量的字面意思是,但仍然无法制定出什么我做错了。

我有一个页面上的多个表单,并希望收集所有表单域,使之成为一个JSON字符串,并将其张贴到PHP页面,其中一个脚本遍历每个项目,并更新相关的数据库表。

这是我的jQuery / JS脚本在所有的形式收集数据:

VAR photo_annotations = {};

  $('表')。每个(函数(一){
    VAR ID = $(本).attr('身份证');
    photo_annotations [ID] = {
        标题:$('#'+ ID +'_标题)VAL()。
        关键字:$('#'+ ID +'_关键字)VAL()
        信用:$('#'+ ID +'_信用)VAL()。
        credit_url:$('#'+ ID +'_ credit_url)VAL()
    };
});
 

如果我的console.log 我photo_annotations对象,这是生产的基础上,两个表单的例子:

  

({11:{标题:标题的第一张照片。关键词:关键字,   关键字2,Keyword3,来源:乔·布洛格斯   credit_url:www.a-domain.com},12:{标题:标题为Lady Gaga的,   关键词:Keyword3,Keyword4,来源:李四,   credit_url:www.another-domain.com}})

然后我需要张贴此作为一个字符串/ JSON到PHP页面,所以我已经做到了这一点:

  $。阿贾克斯({
    键入:POST,
    数据类型:HTML,
    网址:AJAX /保存-annotations.php,
    数据:{数据:JSON.stringify(photo_annotations)},
    的contentType:应用/ JSON的;字符集= UTF-8,
    成功:功能(数据){
        如果(数据){
        $('#form_results')的HTML(数据);
        } 其他 {
        警报(无数据);
        }
    }
});
 

和我的PHP页面,我有这样的:

 < PHP
//的print_r($ _ POST ['数据']);
$日codeD = json_de code($ _ POST ['数据'],真正的);
的print_r($日codeD);
?>
 

现在,这是不是我已经试过了嘛。我试图从AJAX脚本删除所有的JSON的设置,力图只需发送一个纯粹的字符串。我试着删除的contentType和JSON.stringify但还是不会去。我的PHP页面只是不能说我发送的数据。

请帮我推在正确的方向。我已经得到的地步,我不记得我所试过的变化,现在这个脚本是第2天!

设法解决IT

我改写了我的AJAX功能和它的工作。我不知道发生了什么错误的,但决定一个非常基本的数据串测试=你好世界测试我的AJAX功能,并发现没有POST数据可以从PHP读取页面,即使萤火虫说,实际上的页面确实收到数据后符合我的发送。很奇怪。无论如何,这是修改后的AJAX脚本:

  VAR the_obj = JSON.stringify(photo_annotations);
VAR post_data =注解=+ the_obj;
$阿贾克斯({
    网址:AJAX /保存-注释,
    键入:POST,
    数据:post_data,
    数据类型:HTML,
    成功:功能(数据){
        $('#form_results')的HTML(数据);
    }
});
 

解决方案

尝试:

  $。阿贾克斯({
    // ...
    数据:{数据:JSON.stringify(photo_annotations)},
    // ...
  });
 

如果你只是数据属性设置为一个字符串,然后jQuery的认为你想用它作为实际的查询字符串,而当它是JSON的一个blob,明确将无法正常工作。当你通过jQuery的一个的对象的,如上面,然后它会做的属性名称和值(你JSON的blob)相应的URL编码,并为您的查询字符串。你应该得到一个数据参数在服务器上,它的价值将是JSON字符串。

Okay, I'm having some suicidal issues posting a JSON string to a PHP page. I have literally been through the top ten results on Google and plenty of SO questions related to my problem, but still can't work out what I'm doing wrong.

I have multiple forms on a page and want to collect all form fields, turn them into a JSON string and post them to a PHP page, where a script iterates each item and updates the relevant database tables.

This is my jQuery/JS script to collect the data from all the forms:

var photo_annotations = {};

$('form').each(function(i) {
    var id = $(this).attr('id');
    photo_annotations[id] = {
        caption: $('#'+id+'_caption').val(),
        keywords: $('#'+id+'_keywords').val(),
        credit: $('#'+id+'_credit').val(),
        credit_url: $('#'+id+'_credit_url').val()
    };
});

If I console.log my photo_annotations object, this is what is produced, based on a two form example:

({11:{caption:"Caption for first photo.", keywords:"Keyword1, Keyword2, Keyword3", credit:"Joe Bloggs", credit_url:"www.a-domain.com"}, 12:{caption:"Caption for Lady Gaga.", keywords:"Keyword3, Keyword4", credit:"John Doe", credit_url:"www.another-domain.com"}})

I then need to POST this as a string/JSON to a PHP page, so I've done this:

$.ajax({
    type: 'POST',
    dataType: 'html',
    url: 'ajax/save-annotations.php',
    data: { data: JSON.stringify(photo_annotations) },
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        if (data) {
        $('#form_results').html(data);
        } else {
        alert("No data");   
        }
    }
});

And on my PHP page, I've got this:

<?php
//print_r($_POST['data']);
$decoded = json_decode($_POST['data'],true);
print_r($decoded);
?>

Now, this isn't the only thing I've tried. I've tried to remove all the JSON settings from the AJAX script, in a bid to just send a pure string. I've tried removing contentType and JSON.stringify but still won't go. My PHP page just can't get the data that I'm sending.

Please help push me in the right direction. I've got to the point where I can't remember all the variations I've tried and this little script is now on day 2!

MANAGED TO FIX IT

I rewrote my AJAX function and it worked. I have no idea what was going wrong but decided to test my AJAX function with a very basic data string test=hello world and found that no POST data could be read from the PHP page, even though Firebug says that the page did in fact receive post data matching what I sent. Very strange. Anyway, this is the revised AJAX script:

var the_obj = JSON.stringify(photo_annotations);
var post_data = "annotations="+the_obj;
$.ajax({
    url: 'ajax/save-annotations',
    type: 'POST',
    data: post_data,
    dataType: 'html',
    success: function(data) {
        $('#form_results').html(data);
    }
});

解决方案

Try:

  $.ajax({
    // ...
    data: { data: JSON.stringify(photo_annotations) },
    // ...
  });

If you just set the "data" property to a string, then jQuery thinks you want to use it as the actual query string, and that clearly won't work when it's a blob of JSON. When you pass jQuery an object, as above, then it'll do the appropriate URL-encoding of the property names and values (your JSON blob) and create the query string for you. You should get a single "data" parameter at the server, and it's value will be the JSON string.

这篇关于发布JSON字符串到PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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