jquery ajax onerror,javascript - catch jquery ajax error later - Stack Overflow

I try to make an http request to a freebox ( I don't know if it's exist outside france )

The freebox don't access the CORS request, but she make what I will.

For example if I make a request for power :

http://hd1.freebox.fr/pub/remote_control?code=5818260&key=power&long=false&repeat=1

The freebox player start, and I have a CORS error:

XMLHttpRequest cannot load http://hd1.freebox.fr/pub/remote_control?code=5818260&key=power&long=false&repeat=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

It's okay, but I will check the http header for this request, for example if I will check if my code is good, the freebox return 403 for bad code.

I tried to get the statusCode with Jquery :

xhr = $.ajax({

method: "GET",

url: url,

crossDomain: true,

statusCode: {

500: function () {

alert("Une erreur s'est produite !");

},

403: function(){

alert("aaaaa")

throw new badTelecommandeCode();

},

404: function(){

throw new freeboxNotFound();

},

0: function(){console.log(0)}

},

error: function (err) {

console.log(err);

}

});

All this time, the status code is 0, and "error" don't launch. I have this error in console :

XMLHttpRequest cannot load http://hd1.freebox.fr/pub/remote_control?code=5818260&key=power&long=false&repeat=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.

You can see the end of this answer

"The response had HTTP status code 403.".

Just before jquery write an error ( in red ):

GET http://hd1.freebox.fr/pub/remote_control?code=5818260&key=power&long=false&repeat=1

I try to catch the error :

try{

xhr = $.ajax({

method: "GET",

url: url,

crossDomain: true,

statusCode: {

500: function () {

alert("Une erreur s'est produite !");

},

403: function(){

alert("aaaaa")

throw new badTelecommandeCode();

},

404: function(){

throw new freeboxNotFound();

}

},

error: function (err) {

console.log(err);

}

});

}

catch(err){

console.log(err);

}

I have try to search, where the error has catch by jquery :

http://code.jquery.com/jquery-2.1.4.js line 8634

Do You find a way for catch the CORS message, and parse it, or catch the jquery message, and why not get the statusCode or any information .

For the little troller, the way "Send a request to freebox developer for accept CORS" is already make, but no answer. I can't pass by a webserver .

My Current challenge is : "I'm a lambda user will see my TV, but my Free Tv Remote don't work, what I can make easily, with my smartphone/PC"

UPDATE1 :

I have make an example :

if it's work good :

xhr = $.ajax({

method: "GET",

url: "http://hd1.freebox.fr/pub/remote_control?code=5818261&key=right&long=false&repeat=1",

crossDomain: true,

statusCode: {

500: function () {

alert("Une erreur s'est produite !");

},

403: function(){

alert("badTelecommandeCode")

},

404: function(){

alert("freeboxNotFound")

}

},

error: function (err) {

// console.log(err);

},

fail:function(jqxhr, textStatus, errorThrown) {

// console.log(errorThrown);

}

});

Good key, good code, my TV make the action, and return a CORS error

vcUq7.jpg

I will catch, if the code is bad, or if the key doesn't exist :

xhr = $.ajax({

method: "GET",

url: "http://hd1.freebox.fr/pub/remote_control?code=5818261&key=ping",

crossDomain: true,

statusCode: {

500: function () {

alert("Une erreur s'est produite !");

},

403: function(){

alert("badTelecommandeCode")

},

404: function(){

alert("freeboxNotFound")

}

},

error: function (err) {

// console.log(err);

},

fail:function(jqxhr, textStatus, errorThrown) {

// console.log(errorThrown);

}

});

xhr = $.ajax({

method: "GET",

url: "http://hd1.freebox.fr/pub/remote_control?code=222222&key=ping",

crossDomain: true,

statusCode: {

500: function () {

alert("Une erreur s'est produite !");

},

403: function(){

alert("badTelecommandeCode")

},

404: function(){

alert("freeboxNotFound")

}

},

error: function (err) {

// console.log(err);

},

fail:function(jqxhr, textStatus, errorThrown) {

// console.log(errorThrown);

}

});

mdG8m.jpg

I have a CORS, no problem, but how I can catch the text, say the http status .

For information :

freebox.fr is a NAT rules, redirect to my router ( the freebox ), hdX.freebox.fr go to the API for TV player hd number X

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值