javascript - Mockjax dynamic mock stops working with `dataType="Script"` -
i have dynamic mock setup using mockjax, , works of ajax requests, fails when datatype
set script
, , lets request fall through regular ajax handler.
// gets mocked $.ajax({ type: "get", url: "http://myurl.com/myfile.js?_=1395314460347" }) // not mocked! $.ajax({ type: "get", datatype: "script", url: "http://myurl.com/myfile.js?_=1395314460347" })
how can configure dynamic mocks in mockjax intercept requests datatype
set?
update: example code mockjax definition
i creating dynamic mock, defining via function, not plain object, this...
$.mockjax(function(settings) { // settings.url == '/restful/<service>' var service = settings.url.match(/\/restful\/(.*)$/); if ( service ) { return { proxy: '/mocks/' + service[1] + '.json', // handle `datatype: 'script'` datatype: 'application/javascript' }; } return; });
this appears bug how mockjax handles crossdomain script requests. not doing special detect crossdomain request (like jsonp) , such, when passes request original $.ajax
method – jquery never uses mocked xhr object provided mockjax.
so in essence, mockjax intercepting request, , passes right jquery , fails on you.
i opened issue here can fixed: https://github.com/appendto/jquery-mockjax/issues/136
in mean time have 2 choices. if want patch mockjax, add line around 471:
origsettings.crossdomain = false;
that section when done:
mockhandler.cache = requestsettings.cache; mockhandler.timeout = requestsettings.timeout; mockhandler.global = requestsettings.global; origsettings.crossdomain = false; copyurlparameters(mockhandler, origsettings);
the other alternative (which recommend against), adding crossdomain: false
actual ajax request. don't recommend due need remove line when remove mocks later.
thanks @nicholas cloud pinging me , bringing issue attention.
Comments
Post a Comment