-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathdebug.js
193 lines (171 loc) · 4.97 KB
/
debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function showHeaders() {
showAuthHeaders();
showHeaderHeaders();
showParamHeaders();
}
function showAuthHeaders() {
if ($("#authentication").find(".realinputvalue").length > 0) {
$("#addauthbutton").hide();
$("#authentication").show();
} else {
$("#addauthbutton").show();
$("#authentication").hide();
}
}
function showHeaderHeaders() {
if ($("#allheaders").find(".realinputvalue").length > 0) {
$("#allheaders").show();
} else {
$("#allheaders").hide();
}
}
function showParamHeaders() {
if ($("#allparameters").find(".realinputvalue").length > 0) {
$("#allparameters").show();
} else {
$("#allparameters").hide();
}
}
//this specifies the parameter names
$(".fakeinputname").blur(function() {
var newparamname = $(this).val();
$(this).parent().parent().parent().parent().find(".realinputvalue").attr("name", newparamname);
});
$(".close").click(function(e) {
e.preventDefault();
$(this).parent().remove();
showHeaders();
});
$("#addauthbutton").click(function(e) {
e.preventDefault();
if ($("#authentication").find(".realinputvalue").length == 0) {
$('.httpauth:first').clone(true).appendTo("#authentication");
}
showHeaders();
});
$("#addheaderbutton").click(function(e) {
e.preventDefault();
$('.httpparameter:first').clone(true).appendTo("#allheaders");
showHeaders();
});
$("#addprambutton").click(function(e) {
e.preventDefault();
$('.httpparameter:first').clone(true).appendTo("#allparameters");
showHeaders();
});
$("#addfilebutton").click(function(e) {
e.preventDefault();
$('.httpfile:first').clone(true).appendTo("#allparameters");
showHeaders();
});
function postWithAjax(myajax) {
myajax = myajax || {};
myajax.url = $("#urlvalue").val();
myajax.type = $("#httpmethod").val();
if (checkForAuth())
{
myajax.username = $("#authentication input:first").val();
myajax.password = $("#authentication input").eq(1).val();
}
myajax.complete = function(jqXHR) {
$("#statuspre").text(
"HTTP " + jqXHR.status + " " + jqXHR.statusText);
if (jqXHR.status == 0) {
httpZeroError();
} else if (jqXHR.status >= 200 && jqXHR.status < 300) {
$("#statuspre").addClass("alert-success");
} else if (jqXHR.status >= 400) {
$("#statuspre").addClass("alert-error");
} else {
$("#statuspre").addClass("alert-warning");
}
$("#outputpre").text(jqXHR.responseText);
$("#headerpre").text(jqXHR.getAllResponseHeaders());
}
if (jQuery.isEmptyObject(myajax.data)) {
myajax.contentType = 'application/x-www-form-urlencoded';
}
$("#outputframe").hide();
$("#outputpre").empty();
$("#headerpre").empty();
$("#outputframe").attr("src", "")
$("#ajaxoutput").show();
$("#statuspre").text("0");
$("#statuspre").removeClass("alert-success");
$("#statuspre").removeClass("alert-error");
$("#statuspre").removeClass("alert-warning");
$('#ajaxspinner').show();
var req = $.ajax(myajax).always(function(){
$('#ajaxspinner').hide();
});
}
$("#submitajax").click(function(e) {
e.preventDefault();
if(checkForFiles()){
postWithAjax({
headers: createHeaderData(),
data : createMultipart(),
cache: false,
contentType: false,
processData: false
});
} else {
postWithAjax({
headers : createHeaderData(),
data : createUrlData()
});
}
});
function checkForFiles() {
return $("#paramform").find(".input-file").length > 0;
}
function checkForAuth() {
return $("#paramform").find("input[type=password]").length > 0;
}
function createUrlData(){
var mydata = {};
var parameters = $("#allparameters").find(".realinputvalue");
for (i = 0; i < parameters.length; i++) {
name = $(parameters).eq(i).attr("name");
if (name == undefined || name == "undefined") {
continue;
}
value = $(parameters).eq(i).val();
mydata[name] = value
}
return(mydata);
}
function createMultipart(){
//create multipart object
var data = new FormData();
//add parameters
var parameters = $("#allparameters").find(".realinputvalue");
for (i = 0; i < parameters.length; i++) {
name = $(parameters).eq(i).attr("name");
if (name == undefined || name == "undefined") {
continue;
}
if(parameters[i].files){
data.append(name, parameters[i].files[0]);
} else {
data.append(name, $(parameters).eq(i).val());
}
}
return(data)
}
function createHeaderData(){
var mydata = {};
var parameters = $("#allheaders").find(".realinputvalue");
for (i = 0; i < parameters.length; i++) {
name = $(parameters).eq(i).attr("name");
if (name == undefined || name == "undefined") {
continue;
}
value = $(parameters).eq(i).val();
mydata[name] = value
}
return(mydata);
}
function httpZeroError() {
$("#errordiv").append('<div class="alert alert-error"> <a class="close" data-dismiss="alert">×</a> <strong>Oh no!</strong> Javascript returned an HTTP 0 error. One common reason this might happen is that you requested a cross-domain resource from a server that did not include the appropriate CORS headers in the response. Better open up your Firebug...</div>');
}