File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,48 @@ if(typeof XDomainRequest === 'undefined') {
108
108
start ( ) ;
109
109
} ) ;
110
110
} ) ;
111
+
112
+ // Test GET CORS:
113
+ QUnit . asyncTest ( "GET CORS should set content-type header (#187)" , function ( ) {
114
+
115
+ var hasCorrectHeader , restore ;
116
+ restore = makeFixture ( function ( ) {
117
+ this . open = function ( type , url ) {
118
+ } ;
119
+
120
+ this . send = function ( ) {
121
+ this . readyState = 4 ;
122
+ this . status = 200 ;
123
+ this . onreadystatechange ( ) ;
124
+ } ;
125
+
126
+ this . setRequestHeader = function ( header , value ) {
127
+ if ( header === "Content-Type" && value === "application/x-www-form-urlencoded" ) {
128
+ hasCorrectHeader = true ;
129
+ }
130
+ var o = { } ;
131
+ o [ header ] = value ;
132
+ this . responseText = JSON . stringify ( o ) ;
133
+ } ;
134
+ } ) ;
135
+
136
+ ajax ( {
137
+ url : "http://query.yahooapis.com/v1/public/yql" ,
138
+ data : {
139
+ fmt : "JSON" ,
140
+ q : 'select * from geo.places where text="sunnyvale, ca"' ,
141
+ format : "json"
142
+ }
143
+ } ) . then ( function ( response ) {
144
+ QUnit . ok ( hasCorrectHeader , "Content-Type for GET CORS should be set to `application/x-www-form-urlencoded`" ) ;
145
+ restore ( ) ;
146
+ start ( ) ;
147
+ } , function ( err ) {
148
+ QUnit . ok ( false , "Should be resolved" ) ;
149
+ restore ( ) ;
150
+ start ( ) ;
151
+ } ) ;
152
+ } ) ;
111
153
}
112
154
113
155
if ( System . env !== 'canjs-test' && __dirname !== '/' ) {
Original file line number Diff line number Diff line change @@ -159,12 +159,14 @@ module.exports = namespace.ajax = function (o) {
159
159
}
160
160
xhr . open ( type , url ) ;
161
161
162
+ var isJson = o . dataType . indexOf ( "json" ) >= 0 ;
162
163
if ( isPost ) {
163
- var isJson = o . dataType . indexOf ( "json" ) >= 0 ;
164
164
data = ( isJson && ! o . crossDomain ) ?
165
165
( typeof o . data === "object" ? JSON . stringify ( o . data ) : o . data ) :
166
166
$ . _formData ( o . data ) ;
167
167
xhr . setRequestHeader ( "Content-Type" , ( isJson && ! o . crossDomain ) ? "application/json" : "application/x-www-form-urlencoded" ) ;
168
+ } else if ( type === "GET" && o . crossDomain ) {
169
+ xhr . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
168
170
}
169
171
// X-Requested-With header
170
172
xhr . setRequestHeader ( "X-Requested-With" , "XMLHttpRequest" ) ;
Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html >
3
+ < head >
4
+ < title > can-util/dom/ajax/ajax test</ title >
5
+ </ head >
6
+ < body >
7
+ < div id ="qunit-fixture "> </ div >
8
+ < script src ="../../node_modules/steal/steal.js "
9
+ data-main ="can-util/dom/ajax/ajax-test "> </ script >
10
+ </ body >
11
+ </ html >
You can’t perform that action at this time.
0 commit comments