??yy 2013-06-20 13:55 采纳率: 0%
浏览 19

跨域Ajax问题

I have two files 1) index.php(picks data from the code editor and submits for processing via Jquery Ajax to exec.php) 2) exec.php (currently just transfer the data it recieved via index.php using jsonp)

Code of index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function test() {
    var code = document.getElementById('code').value;
    var code_data = "code=" + code;
    alert(code_data);
    $.ajax({
        type: "POST",
        crossDomain: true,
        url: "http://code1.guru99.com/exec.php",
        data: code_data,
        dataType: "jsonp",
        success: function (data) {
            alert(data);
        }

    });
    alert("End of Test");
}
</script> 


<form name="myform" id="myform" method="POST" class="code-box">

<textarea   name="code" id="code"><?
$code='<?php
"Hello";
?>';
echo $code;
?>

</textarea>   <!-- for add html tag in text area nad print the code-->
<div class="hint">This code is editable. Click Run to execute.</div>
<input type="submit" value="Run" id="submit" onClick="test();"><!--<img id="ajax-loader"     name="ajax-loader" src="/img/ajax-loader.gif" class="hidden" style="vertical-align:middle" />-->

</form>
<div name="label" id="label"> </div>
<div name="out" id="out"> </div>

Code of exec.php

<?php
$code=$_POST['code'];
$fp=fopen("file.txt","w"); // Storing the data into a file just to know that data is passed
fwrite($fp,$code);
fclose($fp);
header('Content-Type: application/jsonp');
echo $_GET['callback']."(".json_encode($code).");"
?> 

The problem is data just does not pass into exec.php. I am not sure why... The code is live at http://code.guru99.com/php/ Please help...

  • 写回答

2条回答 默认 最新

  • larry*wei 2013-06-20 20:59
    关注

    You cannot use AJAX to do this. Instead consider posting from a hidden Iframe using a regular FORM and setting the action to the URL you desire. You can still submit the form using JavaScript.

    You can also listen to the onload event on the iframe to detect when your post has completed.

    Alternately, you can use a server-side proxy.

    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办