如何模拟来自Ajax调用的501错误 [英] How to mock a 501 error from ajax call

查看:195
本文介绍了如何模拟来自Ajax调用的501错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过此调用,我在产品中冒出了一个HTTP 501错误:

return $.ajax({
    url: finalUrl,
    success: function (result) {
        console.log('success call result');
        console.log(result);
        finalResult = result;
    },
    data: JSON.stringify(data),
    type: 'PATCH',
    contentType: 'application/json'
});

如何返回模拟错误的模拟,以便可以在生产之外测试修订?我查看了Chrome中的响应"标签,并看到了一条HTML消息:

<HTML><HEAD>
<TITLE>Unsupported Request</TITLE>
</HEAD><BODY>
<H1>Unsupported Request</H1>
PATCH to http&#58;&#47;&#47;demo&#46;site&#46;com&#47;serviceName&#47;v1&#47;requests&#47;9305e338&#45;666a&#45;e611&#45;8516&#45;000c291891bb not supported.<P>
Reference&#32;&#35;8&#46;f0fd717&#46;1472154919&#46;9959c96
</BODY></HTML>

我们怀疑该API根本没有被防火墙阻止.我不知道在这种情况下是否应该使用字符串或对象?如果是对象,那么该对象的成员是什么?

提琴手说:

服务器:AkamaiGHost
Mime版本:1.0
内容类型:text/html
内容长度:350
过期:周四,2016年8月25日20:21:49 GMT
日期:2016年8月25日,星期四,格林尼治标准时间
连接:close

解决方案

只需在您选择的服务器上设置端点,然后使用您选择的Web服务器配置或编码语言发出501响应.但是问题似乎很明显,服务器不希望收到PATCH请求.如果您遇到一些网络问题,则根本不会得到任何答复.

您应该强烈考虑确保您的JavaScript代码既可以处理满意的路径/成功结果,也可以处理其他类型的严重响应结果,以便您的应用程序可以更好地处理其从此类错误中恢复的方式.

I've got an http 501 error bubbling up in prod from this call:

return $.ajax({
    url: finalUrl,
    success: function (result) {
        console.log('success call result');
        console.log(result);
        finalResult = result;
    },
    data: JSON.stringify(data),
    type: 'PATCH',
    contentType: 'application/json'
});

How can I return a mock simulating the error so I can test a fix outside of production? I looked at the response tab in chrome and I see an HTML message:

<HTML><HEAD>
<TITLE>Unsupported Request</TITLE>
</HEAD><BODY>
<H1>Unsupported Request</H1>
PATCH to http&#58;&#47;&#47;demo&#46;site&#46;com&#47;serviceName&#47;v1&#47;requests&#47;9305e338&#45;666a&#45;e611&#45;8516&#45;000c291891bb not supported.<P>
Reference&#32;&#35;8&#46;f0fd717&#46;1472154919&#46;9959c96
</BODY></HTML>

We suspect that the API is not being hit at all, blocked by a firewall. I don't know if I should expect a string or object in this case? If object, then what are the members of that object?

Fiddler says:

Server: AkamaiGHost
Mime-Version: 1.0
Content-Type: text/html
Content-Length: 350
Expires: Thu, 25 Aug 2016 20:21:49 GMT
Date: Thu, 25 Aug 2016 20:21:49 GMT
Connection: close

解决方案

Just set up an endpoint on a server of your choice and use your web server configure or coding language of your choice to issue a 501 response. But the problem seems pretty clear, the server is not expecting a PATCH request. If you had some networking problem you would not be getting a response at all.

you should strongly consider making sure your JavaScript code handles both happy path / success outcome as well as other types off sever response outcomes so your application can better handle how it wants to recover from such errors.

这篇关于如何模拟来自Ajax调用的501错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆