2013-06-20 69 views
0

我有兩個文件 1)的index.php(從代碼編輯精選的數據和處理通過jQuery的Ajax提交給exec.php) 2)exec.php(目前只是將其傳送收到的數據經由的index.php使用JSONP)跨域Ajax的問題​​

的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> 

代碼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).");" 
?> 

的問題是數據只是不通過我nto exec.php。我不知道爲什麼... 代碼是生活在http://code.guru99.com/php/ 請幫助...

+0

請縮進代碼,所以我們可以讀取它。 –

+0

你正在處理三個不同的域,或兩個(包括JavaScript運行的域) –

+2

JSONP不能使用'POST'。 JSONP通過在頁面上添加一個新的'

0

代碼語法是正確的。

可能的問題可能是您的服務器