Im using an ajax call like so:
o.open("POST",q,true);
o.setRequestHeader("Content-type","application/x-www-form-urlencoded");
o.setRequestHeader("Content-length",p.length);
o.setRequestHeader("Connection","close");
Where q = the url and query string.
p = the query string only.
My query takes the form of: "/apps/nettrax/f/events_detail.php?get=1&ids="+multiple values added like this: 123~34567~567~678~etc
This all works if there are a few values, but large value strings fail - the variable ids does not pass (although get is passed)...
* Im not using jquery.
You're sending a POST request, but specifiying the parameters in GET via the URL. There's a limit on the size of URLs, so this won't work. You should be passing the parameters in the send() call, so that they are specified as POST data:
var parameters = "ids=" + encodeURIComponent(ids);
o.open("POST","events_detail.php",true);
o.setRequestHeader("Content-type","application/x-www-form-urlencoded");
o.setRequestHeader("Content-length",p.length);
o.setRequestHeader("Connection","close");
o.send(parameters);
I guess one this two things may be happening:
a) your url string is too long, so it's beeing truncated
b) your parameters are not encoded as the url needs to be, so the string "breaks" the url. if using php use a function like urlencode() or build your own one.
Related
I have a component which uploads an image to Firebase and then receives the image url, which I then store in MongoDB. The problem is I need to store the url as a string. Unformatted, it gets inserted as an object. This is an example url:
https://firebasestorage.googleapis.com/.../sample.jpeg?alt=media&token=474da151-d6...
I use the JSON.stringify method to convert to a string, which then gives me
{"https://firebasestorage.googleapis.com/.../sample.jpeg?alt":"media","token":"474da151-d6..."}
Here, alt=media&token=... has been converted to alt":"media","token":"...
What is best way to return the whole url untouched as a string (without the leading and trailing curly braces as well)? I have used .replace() to remove the curly braces but is there a function/method I should be using to do the whole task simply?
EDIT: Here's some more code:
I pass the url: (it's still a string here according to the console.log)
console.log("url: ",url)
await axios.put(`/api/users/${id}`, url).then...
However, when we goto /api/users.. the req.body is now an object, and if I use json.stringify the 'alt=media&token=' gets changed
case 'PUT':
try {
console.log("req body: ",req.body) // OBJECT
const stringUrl = JSON.stringify(req.body) // SEE BELOW
"https://firebasestorage.googleapis.com/...?alt":"media","token":"..."}
Without Content-Type header specified, your put request treats your string as query parameters. That's why it get broken down into key-value pairs.(e.g. a=b&c=d becomes {a:b, c:d}) You can wrap the URL in a object and send the object in JSON.
await axios.put(`/api/users/${id}`, {url},{headers:{Content-Type:"applicatino/json"}}).then...
Now you will receive a JSON in the req.body, and the url property will be your URL.
P.S. A pure string is technically valid JSON and no need to be wrapped in an object, but not all server implementation accept this. It is safer to send an actual object.
After hours of searching and testing I finally have decided to post a question.
I need to be able to make something like this to work with a function.
var myCSV = [1,3,5,6];
Currently, I can grab the CSV from the database as that is how it is stored, myColumn = 1,3,5,6. I can get it to pass as an AJAX success response but it seems to want to add quotes. I then tried to add JSON encode to my PHP side of things and JSON Parse to the success call but still cannot get the function to work. The end goal is to select checkboxes based off the csv value.
This works
var FilterArray = [1,3,5,6]; // Manually added these numbers as is for testing
$('#myForm').find('.checks').each(function () {
$(this).prop("checked", ($.inArray(parseInt($(this).val()), FilterArray ) != -1));
});
After trying to much to get my AJAX success response to work in the FilterArray, I decided to just pass it to an input value and work with it. However, cannot figure out how to not treat it as a string when I pass it to the function. Here is what I have tried.
In my getCSV.php I have at the end this
json_encode($foundCSV);
In my AJAX Success
var FilterArray = JSON.parse(response);
I have also tried it without the json_encode and just sending it to an input value which does not add quotes.
So in summary, how can I take a csv e.g. 1,3,5,6 stored value and pass it to a function that works as shown above?
Assuming you have your string in a variable, called response, and its value is "1,3,5,6", you can translate it into an array of integers with this:
response.split(',').map(e => parseInt(e))
and you can then pass the result of this to your function.
Sticking to your naming, the code should look like this:
var FilterArray = response.split(',').map(e => parseInt(e));
What the code does:
takes the response and split it by using the comma character as a delimiter
this will create an array of strings
for each string in that array, tries to parse it as an integer (an ID, i guess?)
Clarifying question: What does your $foundCSV look like on the PHP side, before you json-encode it? Is it an array or a string with comma-separated numbers? You might want to check with your developer tools/debugger what the Ajax response contains. It sounds like you have a string but expect an array.
If that's the case, you can create an array on the JS- or PHP-side. For the JS-side, consult #GregorioPalama's answer.
Alternatively, you could do this on the PHP-side, which might be shorter:
$response = explode(',', $foundCSV);
and then work with json_encode($response).
I need to pick up the objects from this service.
http://www.geonames.org/childrenJSON?geonameId=3469034&callback=listPlaces&style=long&noCacheIE=1543532747264
but they are coming in one kind of function js
how can I get the value of the geonames attribute ??
The data format you are looking at is JSONP (which is what we used to work around the Same Origin Policy before CORS was designed).
Remove the callback query string parameter from the URL and the service will return plain JSON instead. You can then parse that as normal.
In the url you have &callback=listPlaces. If you take that out you'll get the actual object. It looks like you're expected to have the name of the callback you passed it defined in your code, and by passing callback as a param its returning the actual function call.
Is it possible to get the result on calling a url and passing the respective parameters along with the url?
For example suppose I pass two nos along with the url and I get the sum back.
The sum is calculated in the called url page
Yeah, data can be sent as URL variables. This is what happens when you use a form which is using "GET"
e.g.
https://www.google.co.uk/?q=example
you are passing "example" as the search term inside the URL
if you want to read up about it properly, have a read through this Post on MDN
I am trying to store digital signature in database via rails application.
I am using javascript plugin that will convert signature into JSON string. Next I pluck JSON string from params and stored in a variable. Now I want to regenerate the signature by giving the plucked JSON string to a reverse javascript function.
The reverse function works fine if I set a variable in javascript directly to the JSON string and pass it to the function. However it does not work when I assign the variable to my rails variable which stores JSON string. The value of JSON string is exactly the same in both cases.
In first case the typeof var shows obj.obj type correctly but in second case it shows as a string. I tired every trick to parse using JSON and Jquery but it does not work.
My rails controller :
#recipient_signature = params[:recipient_signature]
My view where I want to see generated signature using the JSON string.
var sig = "<%=#recipient_signature%>";
$(document).ready(function() {
$('.sigPad').signaturePad({displayOnly:true}).regenerate(sig);
});
The above case does not work even though the value in #recipient_signature is correctly formed JSON string.
But when I assign the same JSON string to sig like below it works...
var sig = [{"lx":55,"ly":25,"mx":55,"my":24},{"lx":55,"ly":25,"mx":55,"my":25}....'];
This JSON string is same as in #recipient_signature and shows up in <%=#recipient_signature%> correctly.
Javascript somehow does not recoganize it as an array in first case and does in second case... how to fix it?
You're wrapping your json in quotes and using the variable in html encoded form:
var sig = "<%=#recipient_signature%>";
Should be
var sig = <%=raw #recipient_signature%>;
Or you should parse the json:
var sig = JSON.parse('<%=raw #recipient_signature%>');
Remove the quotes. That is, change:
var sig = "<%=#recipient_signature%>";
to
var sig = <%=#recipient_signature%>;
That way when the browser receives it it will look like your second example that works.
Note that that isn't really using JSON, it is simply using your server-side code to output a JS array literal directly into the JS code.
My guess is your assignment to sig with double quotes around it is causing a problem - try single quotes.