doukuanyong1939 2012-12-18 03:24
浏览 46
已采纳

将JSON数据发送到PHP SERVER(Localhost到主机站点)

I'm trying to send a JSON data to Server(Localhost to Hosting Site), unfortunately it does not work.

I have this code which I found on this site while searching for a solution and modified it a bit.

proto.html

This is the button to initiate the function

<button onclick="proto_test();">Test Button</button>

This is the code for the function that I'm having a problem with.

function proto_test() {
var data = {},
data['abc'] = [];
data['abc'].push('some info');
data['abc'].push('some more info');
json_data = JSON.stringify(data);

$.post("proto2.php", {'updateValues': json_data}, function(data) {
alert(data);
});

}

proto2.php

Here is the code for the php file which that I'm echoing it.

<?php
echo $_POST['updateValues'];
?>

This is what it looks like after you clicked the button. It alerts the result. (LOCALHOST TO LOCALHOST) In other words, I'm using XAMPP.

Result

Then if I changed the url to pass the data. Which I'm passing it to a SERVER (Hosting Site) It does not work.

$.post("http://www.mysite.com/proto2.php", {'updateValues': json_data}, function(data){
alert(data);
});

Any ideas how to fix this problem? I would be glad if someone helps me.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douba2011 2012-12-18 03:28
    关注

    Read the additional note in jQuery.post() API. AJAX requests are subject to the same origin policy. That's why you can't perform ajax post to different domain.

    If you have control over the target server, you can enable cross-origin resource sharing. Check out this answer on how to enable CORS. Note: CORS is not supported in all browsers. See CORS browser support.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败