The word "Description" causes ajax upload to 403 - ajax

I have been using Ajax for some time successfully in various ways. One use is for uploading of data, using
$.ajax({
cache: false,
type : "POST",
url : encodeURI("savetext.php?..."),
data : {
data: csvData
},
success: function(data3) {},
error: function (xhr, ajaxOptions, thrownError) {}
});
The string csvData is created as lines of semi-colon - separated data, with lines terminated by '\r\n'.
Recently I have had a problem where I receive a 403 Forbidden on doing the upload.
After a long period of investigation [max file size, Cross-Origin, newline characters, ajax() parameters etc] and trial and error, I have concluded that the the problem is simply that I have the string ";Descri" within csvData.
I found this by splicing csvData in various places. ";Descr" seems to be not a problem but ";Descri" does in every position it occurs. I have not identified if it is a substring of ";Descri" that is the problem. I have now solved the problem completely with a kluge by replacing the word 'Description' in my data by 'Designation'.
This seems a very odd situation. Have I done something wrong? What can I do to ensure that I can include ";Descri" within my data in future?

403 issue solution try adding this in htaccess
SecFilterEngine Off
SecFilterScanPOST Off

Because of time I just deleted the word Description from my data, and the problem went away. However a few months later comething similar happened (but with a different trigger text) and I asked this question on the Webmasters site: https://webmasters.stackexchange.com/questions/115890/how-can-file-content-affect-whether-its-uploading-gets-a-403-error
The (unsatisfactory) answer is given there.

Related

$.ajax logs red errors even though .fail is being used

I have a generic function for getting some text data, and it uses $.ajax to try and get this data from several possible locations. This function contains a promise which is returned and is used in later code in various ways among which is pushing several of these results into an array and processing them with Promise.all.
It seems to be going well except for the fact that on some of the items I am getting some red dev console errors - although I'm using a .fail function which supposedly should catch these errors and prevent the red text from appearing in the console.
function getDataText(url, type, locationKey, dataName){
return new Promise((resolve, reject) => {
$.ajax({
url : url,
dataType : type,
timeout: 500
}).fail((err) => {
console.log(`getDataText(): REJECTED!\t${url}`);
reject(new Error(`Could not get '${url}' from location ${locationKey}`));
}).done((data) => {
console.log(`getDataText(): RESOLVED!\t${url}`);
resolve({ data : data, path : url });
});
});
}
What it does is log the red errors first and then it says my "REJECTED" message on the next line. What I'm expecting is no red text at all because I'm supposed to be catching them - my "REJECTED" message should be enough I think.
Is this normal, or am I missing something?
I will also add: this does not happen for all of the rejected items! It happens for exactly these two:
A CORS policy-blocked file, where the URL is a local path starting with "C:..." which is expected at the location for which this is at.
A 404 non-found error for a Mac file-path starting with "/Volumes/" where the ajax request automatically prepends the "http://localhost" part because as I'm running this on my localhost, and it thinks the file will start from there (this was used when testing on simple local HTML file as opposed to served).
Interestingly the rejected items which do not have the red error are both files which were located on other computers and started with "http://[network IP address]". I am wondering if there's not something simple which could be done to help catch those first two...

Receiving and responding to Twilio SMS using parse cloud module

I have a cloud code function that I use to send a text message to a number that is determined in my app (iOS).
The message sends fine, but I want the person who receives the message to be able to respond so that the original sender sees their response. I'm having trouble finding a way to do this using the Parse twilio module.
Here is what my cloud code function looks like right now:
Parse.Cloud.define("sendText", function(request, response) {
// Use the Twilio Cloud Module to send an SMS
twilio.sendSms({
From: "+15555555555",
To: request.params.number,
Body: request.params.message
}, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
});
Is it possible to just call this function again from its callback with the new recipient's number (the number of the original sender)?
Twilio developer evangelist here.
It seems you're trying to create what we call the masked phone numbers feature. This is a feature in which two users communicate through a central Twilio number and never find out each others' details. There is a tutorial on how to accomplish this with both calls and SMS messages however it is in Ruby on Rails. I recommend you take a read through as it gives you a good idea of how you'll need to implement this.
Then, if you have any more Parse/Node.js related questions on how to work on it, please drop me a line at philnash#twilio.com.
Check out this question/answer I posted with a similar issue.
I use Twilio to anonymize calling and texting between users of my app. There's more info in my app about how I do this, but let me know if you have some questions.
If you want to know who sent the message you need the recipient to be able to get the sender's number, so he can call the function again himself.
There are many ways to do that, your idea seems alright, you could add the sender's phone number as a parameter in the cloud function, and use it in the call back when you use sendSMS a second time.
Here is one way to do it :
Parse.Cloud.define("sendText", function(request, response) {
// Use the Twilio Cloud Module to send an SMS
twilio.sendSms({
From: "+15555555555",
To: request.params.recipientnumber, //Notice I changed the paramter name here
Body: request.params.recipientmessage //Notice I changed the paramter name here
}, {
success: function(httpResponse) {
twilio.sendSms({
From: "+15555555555",
To: request.params.sendernumber, //Notice I changed the paramter name here
Body: request.params.sendermessage //Notice I changed the paramter name here
}, {
success: function(httpResponse) {
response.success("SMS sent!");
},
error: function(httpResponse) {
response.error("Uh oh, something went wrong");
}
});
},
error: function(httpResponse) {
response.error("Uh oh, something went wrong");
}
});
});
If the first call succeeds, make another one, if that one succeeds, return success. If either fail, return failure and never start another call.
But make sure you understand this, this will pretty much send both sms at the same time, most of the time. Your sender won't get the feeling that it was a response (or it was a fast one !).
I'm not sure I understand exactly what you're trying to achieve, that's why I posted some comments to suggest something else, but I still wanted to answer here about your exact question.

Google javascript API returns 404's

I have the same code (more or less) working fine in Java, but when I write it in javascript, I end up with 404's. I can't figure out what I'm doing wrong and it's driving me crazy!
gapi.client.load('translate', 'v2', function () {
gapi.client.language.languages.list().execute(function (response) {
response.data.forEach(function(language){
console.log(JSON.stringify(language));
});
});
"language":
{"code":404,"message":"Not Found","data":[{"domain":"global","reason":"notFound","message":"Not Found"}],"error":{"code":404,"message":"Not Found","data":[{"domain":"global","reason":"notFound","message":"Not Found"}]}}
I can see in the console the following POST data to https://content.googleapis.com/rpc?key=MY_API_KEY:
[{"jsonrpc":"2.0","id":"gapiRpc","method":"language.languages.list","apiVersion":"v1"}]
Should that say v1?
By contrast, the REST URL is https://www.googleapis.com/language/translate/v2/languages?key=MY_API_KEY (and it's a GET) and it works fine.
You are right that this was a bug in gapi.client.load. This bug has been fixed and you should no longer run into 404s.

MooTools AJAX Request on unload

i'm trying to lock a row in a db-table when a user is editing the entry.
So there's a field in the table lockthat I set 1 on page load with php.
Then I was trying to unlock the entry (set it 0) when the page is unloaded.
This is my approach. It works fine in IE but not in Firefox, Chrome etc....
The window.onbeforeunload works in all browsers, I tested that.
They just don't do the Request
BUT
if I simple put an alert after req.send(); it works in some browsers but not safari or chrome. So I tried putting something else after it just so that's there's other stuff to do after the request but it doesn't work.
function test() {
var req = new Request({
url: 'inc/ajax/unlock_table.php?unlock_table=regswimmer&unlock_id=',
});
req.send();
alert('bla'); // ONLY WORKS WITH THIS !?!?!?
}
window.onbeforeunload = test;
i've already tried different ways to do the request but nothing seems to work. And the request itself works, just not in this constellation.
ANY help would be appreciated!
Thanks
the request is asynchronous by default. this means it will fork it and not care of the complete, which may or may not come (have time to finish). by placing the alert there you ensure that there is sufficient time for the request to complete.
basically, you may be better off trying one of these things:
add async: false to the request object options. this will ensure the request's completion before moving away.
use an image instead like a tracking pixel.
move over to method: "get" which is a bit faster as it does not contain extra headers and cookie info, may complete better (revert to this if async is delayed too much)
you can do the image like so (will also be $_GET)
new Element("img", {
src: "inc/ajax/unlock_table.php?unlock_table=regswimmer&unlock_id=" + someid + "&seed=" + $random(0, 100000),
styles: {
display: "none"
}
}).inject(document.body);
finally, use window.addEvent("beforeunload", test); or you may mess up mootools' internal garbage collection

IE7 not digesting JSON: "parse error"

While trying to GET a JSON, my callback function is NOT firing.
$.ajax({
type:"GET",
dataType:'json',
url: myLocalURL,
data: myData,
success: function(returned_data) {
alert('success');
}
});
The strangest part of this is that:
my JSON(s) validates on JSONlint
this ONLY fails on IE7...it works in Safari, Chrome, and all versions of Firefox, (and even in IE8). If I use 'error', then it reports "parseError"...even though it validates!
Is there anything that I'm missing? Does IE7 not process certain characters, data structures (my data doesn't have anything non-alphanumeric, but it DOES have nested JSONs)? I have used tons of other AJAX calls that all work (even in IE7), but with the exception of THIS call.
An example data return here is: (this is a structurally-complete example, meaning it is only missing a few second-tier fields, but follows this exact hierarchy)
{"question":{
"question_id":"19",
"question_text":"testing",
"other_crap":"none"
},
"timestamp":{
"response":"answer",
"response_text":"the text here"
}
}
I am completely at a loss. Hopefully someone has some insight into what's going on...thank you!
EDIT
Here's a copy of the SIMPLEST case of dummy data that I'm using...it still doesn't work in IE7.
{
"question":{
"question_id":"20",
"question_text":"testing :",
"adverse_party":"none",
"juris":"California",
"recipients":"Carl Chan"
}
}
I am starting to doubt that it is a JSON issue...but I have NO idea what else it could be. Here are some other resources that I've found that could be the cause, but they don't seem to work either:
http://firelitdesign.blogspot.com/2009/07/jquerys-getjson.html (Django uses Unicode by default, so I don't think this is causing it)
Anybody have any other ideas?
The example data you present looks all right but my strong suspicion still is that there is an unclosed comma somewhere like this:
"timestamp":{
"response":"answer",
"response_text":"the text here"
}, <------------
}
IE is the only browser that (correctly) trips over this.
If this is not it, can you show a full data sample (or confirm that the example you show is indeed a full sample)?
Did you already exclude the possibility of a caching issue?
e.g. you tested with IE7 when myLocalURL returned invalid json. IE7 still caches that response and thus it doesn't work. Try adding something like this (e.g. if php) to myLocalURL or make myLocalURL look like myLocalURL?random=123 just for testing to make sure it isn't a caching thing
header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0");
Are you returning a correct content-typ header? e.g.
header("Content-Type: application/json");
I've just encountered exactly the same issue. It turns out that IE7 fails to parse JSON responses that have leading \r\n line feeds in the response body. Your fix of removing {% load customfilter %} works because you removed the new line that was being included after this tag.
An alternative fix would be to just remove the new line to get
{% load customfilter %}{ "question":{ "question_id":"{{question.id}}",
"question_text":"{{question.question_text|customfilterhere}}"
}
}

Resources