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

博主尝试通过HTTP GET请求控制Freebox TV,但遇到了CORS(跨源资源共享)错误。尽管请求成功执行,浏览器阻止了响应。尝试使用jQuery的statusCode和error/fail回调来捕获403状态码,但始终得到状态码为0。更新中,博主展示了如何根据返回的CORS错误判断遥控器指令是否正确,但依然无法获取到具体的HTTP状态码。问题在于Freebox不支持CORS,而直接请求会导致浏览器安全限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值