mui访问的接口问题?

1.想了老半天,为什么调用的接口不能。
首先我们要区分本机地址和服务器的地址:
a.localhost请求的是本地主机地址,
b.47.98.32.209 请求的是服务器地址。
了解一下常识:
localhost是什么意思:

一个特殊的DNS主机名, 代表分配给引用这个名称的计算机的IP地址。 把这个名称看作是提供快速但是不可靠服务的无连接协议。 localhost 指你所在的计算机本身。 在windows系统它成了 127.0.0.1的别名 ,在*nix系统下,查看网卡配置会发现作为本地回环的方式,一定程序上使用localhost比127.0.0.1要快一些。 重要重申localhost和IIS,IE,windows无关,他就是指你使用的计算机本身。 在Hosts文件中 localhost指向的IP是127.0.0.1 这个关系是可以修改的 localhost login 是什么意思? 在Linux系统中,系统管理员在为用户建立新帐号时赋给用户一个用户名和一个初始的口令。另外,Linux系统给计算机赋予一个主机名。主机名用于在网络上识别独立的计算机(即使用户的计算机没有联网,也应该有一个主机名)。TurboLinux系统给出的缺省主机名为:localhost。 在Linux系统成功启动后,屏幕会提示:localhost login ,意思就是系统要求输入用户名,接着要求输入口令。否则将无法登录系统,也就无法使用。

服务器端:
由于服务器在网络中提供服务,那么这个服务的质量对承担多种应用的网络计算环境是非常重要的,承担这个服务的计算机硬件必须有能力保障服务质量。这个服务首先要有一定的容量,能响应单位时间内合理数量的服务器请求,同时这个服务对单个服务请求的响应时间要尽量快,还有这个服务要在要求的时间范围内一直存在。
如果一个WEB服务器只能在1分钟里处理1个主页请求,1个以外的其他请求必须排队等待,而这一个请求必须要3分钟才能处理完,同时这个WEB服务器在1个小时以前可以访问到,但一个小时以后却连接不上了,这种WEB服务器在现在的Internet计算环境里是无法想象的。
现在的WEB服务器必须能够同时处理上千个访问,同时每个访问的响应时间要短,而且这个WEB服务器不能停机,否则这个WEB服务器就会造成访问用户的流失。
为达到上面的要求,作为服务器硬件必须具备如下的特点:性能,使服务器能够在单位时间内处理相当数量的服务器请求并保证每个服务的响应时间;可靠性,使得服务器能够不停机;可扩展性,使服务器能够随着用户数量的增加不断提升性能。因此我们说不能把一台普通的PC作为服务器来使用,因为,PC远远达不到上面的要求。这样我们在服务器的概念上又加上一点就是服务器必须具有承担服务并保障服务质量的能力。这也是区别低价服务器和PC的差异的主要方面。
在信息系统中,服务器主要应用于数据库和Web服务,而PC主要应用于桌面计算和网络终端,设计根本出发点的差异决定了服务器应该具备比PC更可靠的持续运行能力、更强大的存储能力和网络通信能力、更快捷的故障恢复功能和更广阔的扩展空间,同时,对数据相当敏感的应用还要求服务器提供数据备份功能。而PC机在设计上则更加重视人机接口的易用性、图像和3D处理能力及其他多媒体性能。

在mui中,如果你用的是本地本机访问,他会得不了数据,只有发布成功后利用服务器访问才能得到数据。

来个实例吧。

看代码:

<script src="../js/mui.min.js"></script>
		<script type="text/javascript">
			mui.plusReady(function() {
			         getToDoNum();
			});   
			 function getToDoNum(){  
			     mui.ajax("http://localhost:60216/api/User", {
			         data: {
			                 task_tenantId:"f0fd6e1f-cf85-11e7-98b1-fa163e00a2e7",
			                 task_formKey:"pid-88fa484b-2d67-4e6e-8967-b9521cc5ef52"
			             },
			        dataType: 'json',
			        contentType: 'application/json;charset=utf-8', //服务器返回json格式数据
			        type: 'get', //HTTP请求类型
			        timeout: 10000, //超时时间设置为10秒;
			        headers:{"Authorization":"Basic YWRtaW46YWRtaW4="},    //接口权限,用户名和密码   
			        success: function(result,status,xhr) {
			            console.log(JSON.stringify(result))
			            document.getElementById("response").innerHTML=result;
			        },   
			        error: function(xhr,status,err) {   
			            mui.each(xhr,function(i){  
			                console.log(xhr[i]) 
			           })
			            console.log(err)
			            plus.nativeUI.closeWaiting();
			        }
			    });
			}
		</script>
		 

看一下返回的数据:

23:01:32.763 正在同步/sub1/new_file.html文件...
23:01:33.039 1183 KB/s (3550 bytes in 0.002s)
23:01:33.064 文件/sub1/new_file.html同步完成
23:01:33.441 function(a,b){r[a.toLowerCase()]=[a,b]} at sub1/new_file.html:94
23:01:33.464 0 at sub1/new_file.html:94
23:01:33.485 1 at sub1/new_file.html:94
23:01:33.507 2 at sub1/new_file.html:94
23:01:33.526 3 at sub1/new_file.html:94
23:01:33.546 4 at sub1/new_file.html:94
23:01:33.567 function(){} at sub1/new_file.html:94
23:01:33.587 4 at sub1/new_file.html:94
23:01:33.609 0 at sub1/new_file.html:94
23:01:33.628 false at sub1/new_file.html:94
23:01:33.649 [object XMLHttpRequestUpload] at sub1/new_file.html:94
23:01:33.669  at sub1/new_file.html:94
23:01:33.690 0 at sub1/new_file.html:94
23:01:33.714  at sub1/new_file.html:94
23:01:33.731  at sub1/new_file.html:94
23:01:33.751  at sub1/new_file.html:94
23:01:33.780  at sub1/new_file.html:94
23:01:33.801 null at sub1/new_file.html:94
23:01:33.825 function open() { [native code] } at sub1/new_file.html:94
23:01:33.848 function send() { [native code] } at sub1/new_file.html:94
23:01:33.893 function abort() { [native code] } at sub1/new_file.html:94
23:01:33.912 function getResponseHeader() { [native code] } at sub1/new_file.html:94
23:01:33.938 function getAllResponseHeaders() { [native code] } at sub1/new_file.html:94
23:01:33.953 function overrideMimeType() { [native code] } at sub1/new_file.html:94
23:01:33.974 null at sub1/new_file.html:94
23:01:33.994 null at sub1/new_file.html:94
23:01:34.015 null at sub1/new_file.html:94
23:01:34.035 null at sub1/new_file.html:94
23:01:34.056 null at sub1/new_file.html:94
23:01:34.076 null at sub1/new_file.html:94
23:01:34.097 null at sub1/new_file.html:94
23:01:34.117 function addEventListener() { [native code] } at sub1/new_file.html:94
23:01:34.138 function removeEventListener() { [native code] } at sub1/new_file.html:94
23:01:34.158 function dispatchEvent() { [native code] } at sub1/new_file.html:94
23:01:34.179 null at sub1/new_file.html:96

报错吧,

发布后用服务器再次请求:

<script src="../js/mui.min.js"></script>
		<script type="text/javascript">
			mui.plusReady(function() {
			         getToDoNum();
			});   
			 function getToDoNum(){  
			     mui.ajax("http://47.98.32.209:8084/api/User", {
			         data: {
			                 task_tenantId:"f0fd6e1f-cf85-11e7-98b1-fa163e00a2e7",
			                 task_formKey:"pid-88fa484b-2d67-4e6e-8967-b9521cc5ef52"
			             },
			        dataType: 'json',
			        contentType: 'application/json;charset=utf-8', //服务器返回json格式数据
			        type: 'get', //HTTP请求类型
			        timeout: 10000, //超时时间设置为10秒;
			        headers:{"Authorization":"Basic YWRtaW46YWRtaW4="},    //接口权限,用户名和密码   
			        success: function(result,status,xhr) {
			            console.log(JSON.stringify(result))
			            document.getElementById("response").innerHTML=result;
			        },   
			        error: function(xhr,status,err) {   
			            mui.each(xhr,function(i){  
			                console.log(xhr[i]) 
			           })
			            console.log(err)
			            plus.nativeUI.closeWaiting();
			        }
			    });
			}
		</script>

返回的竟然是正确的数据:

23:06:07.809 正在同步/sub1/new_file.html文件...
23:06:08.003 1212 KB/s (3637 bytes in 0.002s)
23:06:08.017 文件/sub1/new_file.html同步完成
23:06:08.479 [{"uid":1,"name":"张三1"},{"uid":2,"name":"张三2"},{"uid":3,"name":"张三3"},{"uid":4,"name":"张三4"},{"uid":5,"name":"张三5"},{"uid":6,"name":"张三6"},{"uid":7,"name":"张三7"},{"uid":8,"name":"张三8"}] at sub1/new_file.html:90

今天一直在想这个问题,为什么访问不了,原来就错在这里。

写程序真的难啊,但确实是挺有趣的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值