【发布时间】:2014-03-25 09:48:45
【问题描述】:
我正在尝试掌握 javascript 承诺的概念。但我遇到了一些问题。我在本地设置了一个很小的 web 服务(不要生气,web 服务不符合约定)。这里有一些关于它的细节
/login/<username>/<password> ==> 登录系统,正确的用户名和密码都是noor
如果用户登录,可以拨打/car/<brand>/<color>/<plate_number>,
我没有对颜色、品牌、车牌号的类型进行任何验证
这个很好用,我正在记录并添加一辆车
$.ajax({type: "GET",url: url+"/login/noor/noor"})
.then(function( data, textStatus, jqXHR ) {console.log("login success");},function(){console.log("login error");})
.then($.ajax({type: "GET",url: url+"/car/1/1/1"}))
.then(function(){console.log("car added");},function(){console.log("car not added");});
这个完美显示错误,因为使用了无效的url:
$.ajax({type: "GET",url: url+"/carasdsad/1/1/1"})
.then(function(){console.log("car added");},function(){console.log("car not added");});
"/carasdsad/1/1/1" 是一个无效的 url 并且 car not added 被返回
我遇到了这个问题。下面的代码使用上面的代码。我期待 car not added 会显示,但它的显示 car added
$.ajax({type: "GET",url: url+"/login/noor/noor"})
.then(function( data, textStatus, jqXHR ) {console.log("login success");},function(){console.log("login error");})
.then($.ajax({type: "GET",url: url+"/carasdsad/1/1/1"}))
.then(function(){console.log("car added");},function(){console.log("car not added");});
虽然 "/carasdsad/1/1/1" 在第二次调用中是无效的 url,但上述代码返回 car added。
【问题讨论】:
标签: javascript jquery promise