I'm trying to send stringified JSON to a local address using jQuery AJAX.
This address is waiting to receive JSON data.
However, once sent, it returns "400 Bad Request".
//response received as: {"success":"true","packages":["https://example.com/link1.pkg","https://example.com/link2.pkg","https://example.com/link3.pkg"]}
var res = JSON.parse(response);
var packages = [];
$.each(res.packages, function (i, item) { // put links in array
packages.push(item);
});
if (res.success) {
$.ajax({
url: 'http://192.168.2.10:12800/api/install',
type: 'POST',
headers: {
'Access-Control-Allow-Origin': '*', // cors
'Content-Type': 'application/json'
},
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
data: JSON.stringify({
'type': 'direct',
'packages': packages
}),
success: function (response) {
$('#dynamicModal').find('#response').html('<p>Packages sent!</p>');
},
error: function (response) {
$('#dynamicModal').find('#response').html('<p>Error occurred while sending.</p>');
}
});
} else {
$('#dynamicModal').find('#response').html('<p>Error occurred while gathering links.</p>');
}
I've checked so many links on Google, and 90% say the JSON isn't stringified, but mine is. I'm out of ideas and I'm hoping someone could help me out and potentially see the issue.
This is the full console error:
GET http://192.168.2.10:12800/api/install?callback=jQuery3410603622444131205_1578188027542&{%22type%22:%22direct%22,%22packages%22:[%22http://example.com/EXAMPLE_0.pkg%22,%22http://example.com/EXAMPLE_1.pkg%22,%22http://example.com/EXAMPLE_2.pkg%22,%22http://example.com/EXAMPLE_3.pkg%22,%22http://example.com/EXAMPLE_4.pkg%22,%22http://example.com/EXAMPLE_5.pkg%22,%22http://example.com/EXAMPLE_6.pkg%22]}&_=1578188027543 net::ERR_ABORTED 400 (Bad request)
Update
I've found the code where it fails on the local server:
static int event_handler(sb_Event* e) {
const struct handler_desc* descs = NULL;
handler_cb* handler = NULL;
char* in_data;
size_t in_size;
size_t count;
size_t i;
int ret;
if (e->type != SB_EV_REQUEST) {
ret = SB_RES_OK;
goto done;
}
if (strcasecmp(e->method, "GET") == 0) {
descs = s_get_handlers;
count = ARRAY_SIZE(s_get_handlers);
} else if (strcasecmp(e->method, "POST") == 0) {
descs = s_post_handlers;
count = ARRAY_SIZE(s_post_handlers);
}
if (!descs) {
bad_request:
kick_error(e->stream, 400, "Bad request", "Unsupported method");
ret = SB_RES_OK;
goto done;
}
for (i = 0; i < count; ++i) {
if (descs[i].need_partial_match) {
if (strstr(e->path, descs[i].path) == e->path) {
handler = descs[i].handler;
break;
}
} else {
if (strcmp(e->path, descs[i].path) == 0) {
handler = descs[i].handler;
break;
}
}
}
if (!handler) {
goto bad_request;
}
in_data = sb_get_content_data(e->stream, &in_size);
(*handler)(e->stream, e->method, e->path, in_data, in_size);
ret = SB_RES_OK;
done:
return ret;
}
To me this looks like it's not able to get s_get_handlers
or s_post_handlers
?