高手过来看看这个问题怎么解决,谢谢!
<!DOCTYPE HTML>
<html>
<head>
<title>jsontest_1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#button').click(function(){
$.getJSON('http://aazj.cn:8080/minisns/logon.json', function(data){
$('#result').append(data);
});
});
});
</script>
</head>
<body>
<div>点击按钮获取JSON数据</div>
<input type="button" id="button" value="确定" />
<div id="result"> </div>
</body>
</html>
点击按钮后,浏览器的js控制台出现了如下的错误提示
XMLHttpRequest cannot load http://aazj.cn:8080/minisns/logon.json. Origin null is not allowed by Access-Control-Allow-Origin.
该怎么改啊??
还有服务器不在我本地, 我也看不了服务器上的action是怎么写的
还有我要发参数到服务器怎么添加呢??
因为我用表单提交时服务器会返回json格式的数据给我,表单如下:
<!DOCTYPE>
<html>
<head>
<title>Test Logon</title>
</head>
<body>
<form action="http://aazj.cn:8080/minisns/logon.json" method="post">
<table>
<tr>
<td> </td>
<td>logon</td>
</tr>
<tr>
<td> <div align="right">mobile(String)</div> </td>
<td> <input type="text" name="mobile"/> </td>
</tr>
<tr>
<td> <div align="right">password(String)</div> </td>
<td> <input type="text" name="password" /> </td>
</tr>
<tr>
<td> <div align="right">code(String)</div> </td>
<td> <input type="text" name="code" /> </td>
</tr>
<tr>
<td> </td>
<td> <input type="submit" id="button" value="提交" /> <td>
</tr>
</table>
</form>
</body>
</html>
点击提交按钮后服务器发给我的信息是内容如下的.json文件
{"rs":"s","body":{"ukey":"6C99FBB0E098C9F89EA53DEA748ACEA8","token":"OJo6E1jC87fd8MxX","userId":"116"},"pl":6}
有点绕了,说简单点,我其实就是想把用表单提交的方式改为ajax的形式,因为这样从服务器发过来的json数据我好用javascript来处理,并将其内容在页面上展示出来。
[解决办法]
看下这个:http://www.ghugo.com/origin-null-is-not-allowed-by-access-control-allow-origin/
[解决办法]
可以尝试使用WinSCP来连接服务器。。查看服务器的代码。。。
提供一个例子代码吧
<script type ="text/javascript" src ="../js/jquery.js"></script> //必须要引用
<script type ="text/javascript" src ="../js/jquery.form.js"></script> //必须要引用
<script type="text/javascript">
// wait for the DOM to be loaded
$(document).ready(function()
{
$('#Tip').hide();//显示操作提示的元素不可见
$('#form1').submit(function()//提交表单
{
//alert("ddd");
var options = {
target:'#Tip', //后台将把传递过来的值赋给该元素
url:'ReturnVisit.aspx?flag=do', //提交给哪个执行
type:'POST',
success: function(){ alert($('#Tip').text());} //显示操作提示
};
$('#form1').ajaxSubmit(options);
return false; //为了不刷新页面,返回false,反正都已经在后台执行完了,没事!
});
}
);
</script>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" class="tableCategory">客户回访</td>
</tr>
<tr>
<td width="30%" class="tableBg1">客户名称:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_CorpName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1">回访主题:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_ReturnVisitTitle" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1">联系人:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_ContractPerson" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1">联系人职务:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_ContractPersonPosition" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1">联系电话:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_ContractPhone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1">回访时间:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_ReturnVisitDate" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1">回访内容:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_ReturnVisitContent" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1">回访相关文档:</td>
<td class="tableBg2">
<asp:TextBox ID="txtF_ReturnVisitFile" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1"> </td>
<td class="tableBg2">
<asp:Button ID="Submit_BT" runat="server" Text="确定" CssClass="button" />
<input type="reset" class="button" value="还原" />
</td>
</tr>
</table>
<span id="Tip"></span>
</div>
</form>
</body>
<td class="tableBg2">
<asp:TextBox ID="txtF_ReturnVisitFile" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="30%" class="tableBg1"> </td>
<td class="tableBg2">
<asp:Button ID="Submit_BT" runat="server" Text="确定" CssClass="button" />
<input type="reset" class="button" value="还原" />
</td>
</tr>
</table>
<span id="Tip"></span>
</div>
</form>
</body>
详细出处参考:http://www.jb51.net/article/21051.htm
[解决办法]
$.ajax({
async:false,
method:"get",
url:url,
dataType:"jsonp",
success:function(data){
}
})