I have a form which has two input fields. One of them which allows white spaces gets value from the user. The second hidden input passes all data from the first input with removing all-white spaces.
I would like to send the second input's data to the ajax request as it doesn't have any white spaces. After that, I would like to control this data with API.
In ajax request, when I use data: $('#firstInput').serialize(),
API returns null, if the user wants to use white spaces in the input.
When I use JSON.stringify()
, I get a cross-origin frame error:
Blocked a frame with origin "http://localhost.dev" from accessing a cross-origin frame.`
Using Access-Control-Allow-Headers
etc. doesn't work. To be able to overcome this problem, after some researches, I decided to use param()
and split()
functions. In this way, I get the result I want in console.log()
.
How can I send the last split object to the ajax request in the data field?
var firstInput = $('#firstInput').val();
var trimInput = firstInput.replace(/\s+/g, '');
var hiddenInput = $('#hiddenInput').val(trimInput);
var paramFunc = jQuery.param(hiddenInput);
var splitFunc = paramFunc.split("=");
var result = splitFunc[1];
$.ajax({
url: url,
type: "POST",
data: JSON.stringify(hiddenInput), //Gives: Blocked a frame with origin "http://localhost.dev" from accessing a cross-origin frame error
data: {"data": result}, // It returns null
success: function (response) {
console.log(response);
},
error: function (err) {
console.log(err);
}
});