weixin_33725272 2016-07-14 04:14 采纳率: 0%
浏览 30

提交JSONP到PHP

As part of my application, I want to submit a JSON object to a PHP script which returns a JSON response. The domain is not following the same-origin policy, i.e., the JSON is submitted from a different domain, so after doing some research, I understand that I need to use JSONP. After spending more than 10 hours now, I can't think of any other solution. Can anyone help me to understand where my problem is?

Javascript snippet:

var jsondata = {
        "variablA": "valueA",
        "variables": {
            "variableB": "valueB",
            "variableC": "valueC"
        };

        var encodedjson = JSON.stringify(jsondata);



        $.ajax({
            url: 'update.php',
            type: 'POST',
            contentType: 'application/json',
            dataType: "jsonp",
            data: {
                "data": encodedjson
            },
            success: function(data) {
                alert(data);
            },
            error: function(xhr, ajaxOptions, thrownError) {
                //On error do this
                $.mobile.loading('hide')
                if (xhr.status == 200) {

                    alert(ajaxOptions);
                } else {
                    alert(xhr.status);
                    alert(thrownError);
                }
            }
        });

PHP:

$array = json_decode($_REQUEST['data'], true);

$valueA = $array['variableA'];
$valueB = $array['variables']['variableB'];
$valueC = $array['variables']['variableC'];


$values = array('variableB' => $valueB, 'variableC' => $valueC);

$respencode = array('variableA' => $valueA, 'variables' => $values);

header('Content-type: application/json');
echo json_encode($respencode);

</div>
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 如何降低hdfs中datanode的JVM内存用量
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver