| 849 | * - returns the corresponding response |
| 850 | */ |
| 851 | function ajaxHandleResponses( s, jqXHR, responses ) { |
| 852 | |
| 853 | var contents = s.contents, |
| 854 | dataTypes = s.dataTypes, |
| 855 | responseFields = s.responseFields, |
| 856 | ct, |
| 857 | type, |
| 858 | finalDataType, |
| 859 | firstDataType; |
| 860 | |
| 861 | // Fill responseXXX fields |
| 862 | for ( type in responseFields ) { |
| 863 | if ( type in responses ) { |
| 864 | jqXHR[ responseFields[type] ] = responses[ type ]; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | // Remove auto dataType and get content-type in the process |
| 869 | while( dataTypes[ 0 ] === "*" ) { |
| 870 | dataTypes.shift(); |
| 871 | if ( ct === undefined ) { |
| 872 | ct = s.mimeType || jqXHR.getResponseHeader( "content-type" ); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | // Check if we're dealing with a known content-type |
| 877 | if ( ct ) { |
| 878 | for ( type in contents ) { |
| 879 | if ( contents[ type ] && contents[ type ].test( ct ) ) { |
| 880 | dataTypes.unshift( type ); |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | // Check to see if we have a response for the expected dataType |
| 887 | if ( dataTypes[ 0 ] in responses ) { |
| 888 | finalDataType = dataTypes[ 0 ]; |
| 889 | } else { |
| 890 | // Try convertible dataTypes |
| 891 | for ( type in responses ) { |
| 892 | if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) { |
| 893 | finalDataType = type; |
| 894 | break; |
| 895 | } |
| 896 | if ( !firstDataType ) { |
| 897 | firstDataType = type; |
| 898 | } |
| 899 | } |
| 900 | // Or just use first one |
| 901 | finalDataType = finalDataType || firstDataType; |
| 902 | } |
| 903 | |
| 904 | // If we found a dataType |
| 905 | // We add the dataType to the list if needed |
| 906 | // and return the corresponding response |
| 907 | if ( finalDataType ) { |
| 908 | if ( finalDataType !== dataTypes[ 0 ] ) { |