2012-11-16 42 views
0

下面的代码不能将数据发送到其他服务器。我想用$ .ajax发送"aaa-bbb-ccc"。但发布后userCode这是从$ _POST发布空数据。对不起,我的英语

jQuery代码:

<script type="text/javascript"> 
$(function(){ 
    $.ajax({ 
     url: "http://www.site.com/index.php", 
     type: "POST", 
     dataType: "jsonp", 
     data: {userCode: "aaa-bbb-ccc"} 
    }).done(function(data){ 
     alert(data.message); 
    }); 
}); 
</script> 

服务器的index.php:

<?php 
include_once ('./AFactory.class.php'); 
$database= new AFactory; 
$db=new AFactory(); 
$link=$db->getDBO(); 
if ($_POST['userCode'] == '') 
{ 
    $data['success']=false; 
    $data['message']='ERROR ...'; 
} 
else { 
    $query=array('id'=>NULL,'userCode'=>$_POST['userCode']); 
    $sql=$db->insertQuery('`alachiq_takhmis`.`users`',$query); 
    if (mysql_query($sql)) 
     { 
      $data['success']=true; 
      $data['message']=$_POST['userCode']; 
     } 
    else 
     { 
      $data['success']=false; 
      $data['message']=$_POST['userCode']; 
     } 
} 
echo $_GET['callback'] . '('. json_encode($data) . ')'; 
?> 

后回:

({"success":false,"message":'ERROR ...'}) 

什么我的代码的问题?

回答

1

JSONP通过将<script>元素与src属性注入到文档中工作。

那只能做出GET请求。

+0

如何修改此代码? –

+0

重写它以使用GET或XHR。 – Quentin

0
$.ajax({ 
    url: "http://www.site.com/index.php", 
    type: "GET", 
    dataType: "jsonp", 
    data: {userCode: "aaa-bbb-ccc"} 
});