I have a form whose data is asynchronously uploaded to Blobstore. When the upload is completed, the backend page outputs another blobstore.get_upload_url() to use for a second upload.
However, when the second upload is performed, it gives a POST http://localhost:13080/_ah/upload/ah1kZXZ-YXZpc2JlcmdhbW8tMTQ2OTk2MTI5MTQ0OXIiCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGICAgICA_IoLDA 404 (Not Found) error
Here's the code (JS):
$.ajax({
url: upload_url,
data: formData,
contentType: false,
processData: false,
success: function (data) {
upload_url = data.url;
formData = new FormData();
},
async: true,
method: 'POST',
});
Here's the backend:
class InserisciDomandaHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
# ... uploads additional data to Datastore
self.response.headers['Content-Type'] = 'application/json'
obj = {
'url': blobstore.create_upload_url('/upload_photo'),
}
self.response.out.write(json.dumps(obj))
I checked the new url and it is different than the old one. Additionally, the new url seems valid.
Why does it give me a 404 error? Thanks
EDIT:
The url seems valid but, when used both in production and in local, it is not treated as valid by App Engine. So, although it "intuitively" makes sense, it doesn't make sense in practice.
Related
I am attempting to recover a WordPress user's ID on the backend PHP after receiving an AJAX post request.
I was unable to use wp_get_current_user() within my AJAX handler function on the backend (I'm not entirely sure why this fails)
My solution was to use wp_localize_script to pass wp_get_current_user()->ID to the browser. I am concerned that the user might be able to change the user ID within their browser and pose as another user. Is this possible? If so, is there a better way to get the user's ID in an ajax POST?
The following is my current setup:
JS Ajax
var fd = new FormData();
fd.append('user_id', ajax_object.user_id);
fd.append('action', 'tb_upload');
jQuery.ajax({
type: "POST",
async: false,
url: ajax_object.ajaxurl,
data: fd,
contentType: false,
processData: false,
success: function (resp) {
console.log("in success!");
if (resp != null) {
console.log(resp.data);
} else {
console.log("Error: Response is null!");
}
}
});
Inside my flask python web program, I save a few parameters in SessionStorage in order to send them back to flask, then save this information as a txt file.
For some reason, everything works flawlessly on PC and Android, couldn't get it to work on iOS devices. it recognizes and saves the sessionStorgae elements before sending, but no txt file is created afterwards (at the end of the app).
Client-side (HTML):
function func()
{
...
$.ajax({
url: "{{ url_for('getInfo', _external=True) }}",
type: 'POST',
dataType: 'json',
contentType: 'application/json;charset=UTF-8',
success: function (response) {
console.log(response);
},
accepts: {
json: 'application/json',
},
data: JSON.stringify(log)
});
return;
...
}
<form id = "myForm" name ="myForm" method="post" action="{{url_for('final')}}" onsubmit="return func()">
Server-side (Flask):
#app.route("/get_info",methods = ['POST'])
def getInfo():
list = request.get_json()
global id
with open(id + '.txt', 'w+') as outfile:
json.dump(list,outfile,indent=2)
return 'OK'
I can't figure out the solution. Also I can't remember if it used to work or not on iOS. trying to run all kinds of tests as I'm writing this. Any help would be much appreciated. Thanks.
If you believe more information is needed, I elaborated in the comments about the overall structure of that HTML page and web program.
Your code in 'DEBUG' mode
#app.route("/get_info",methods = ['POST'])
def getInfo():
list = request.get_json()
global id
file_name = '{}.txt'.format(id)
print('About to write {} to file {}'.format(str(list),file_name))
with open(file_name, 'w+') as outfile:
json.dump(list,outfile,indent=2)
print('Data was written')
return 'OK'
Found an answer for future readers:
iOS puts ajax request in cache. which later on might give empty response, or don't trigger the function at all. you can solve that by adding a few parameters and headers to the ajax request, which will prevent it from happenning:
cache: false,
processData: false,
async: true,
headers: {
"cache-control": "no-cache"
},
Forgive me if I have titled this tread wrong, I don't know the correct terminology.
I have a link with values included, they are: id and environment therefore the url it formatted such ad https://foo.com?id=1234&e=s
With this url I need to take the id and the environment (Stage or production) and create a post body to set a flag as true.
So if e=s (environment = stage) I need post to
https://services-stage.foo.com/api/account//flag
Or if e = p (production)
https://services.foo.com/api/account//flag
Where = the id in the url
With POST body:
{
"type": OptionFlag,
"flag": true //Flag should be set to true
}
My problem is I don't know where to start with my limited javascript knowledge. I have created GET requests form api's in the past but never POST and never with parameters in the URL.
I now understand how to handle the post, however I am still at a loss on how I would get the Id and the environment data from the url to then pass them to the ajax url in the function.
function CustomerId(){
//Function for getting id number from URL
}
function apiEnvironment(){
//Function for getting environment from URL
}
function OptOut(CustomerId, apiEnvironment) {
jQuery.ajax({
type: 'POST',
url: apiEnvironment + '/api/v1/account/' + CustomerId + '/optOut',
dataType: 'json',
contentType: 'application/json; charset=UTF-8',
data : data,
success: {
}
});
}
This Is what I have so far...
Submitted.html
The JS is also surrounded by a document.ready function
var uploader = new qq.FileUploader({
action: "{% url 'QCOMLTE:imager' %}",
element: $('#file-uploader')[0],
multiple: true,
onComplete: function(id, fileName, responseJSON) {
if(responseJSON.success) {
alert("success!");
} else {
alert("upload failed!");
}
},
onAllComplete: function(uploads) {
// uploads is an array of maps
// the maps look like this: {file: FileObject, response: JSONServerResponse}
alert("All complete!");
},
params: {
'csrf_token': "{{ csrf_token }}",
'csrf_name': 'csrfmiddlewaretoken',
'csrf_xname': 'X-CSRFToken'
}
});
elsewhere in the html body
<div id="file-uploader">
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
</noscript>
</div>
urls.py
urlpatterns = patterns('',
url(r"^Submitted/$", views.HybridDetailView.as_view(), name='Submitted'),
url(r'^(?P<object_type>\w+)/process/$', views.process, name='process'),
url(r'^(?P<object_type>\w+)/$', views.generic_view, name='generic'),
url("$^", views.head, name='head'),
url("uploader", views.upload, name= 'imager'),
)
views.py
#AjaxFileUploader
def upload(request):
response = {'files': []}
script_dir = os.path.dirname(__file__)
# Loop through our files in the files list uploaded
print('request',request)
print(request.FILES)
for item in request.FILES.getlist('files[]'):
file = UploadedFile(item)
with open(script_dir + '/Excel/' + file.name) as destination:
for chunk in file.chunks():
destination.write(chunk)
response['files'].append(file)
print('appended')
return HttpResponse(json.dumps(response), content_type='application/json')
also contains 'ajaxuploader' in the installed apps list
When I try to submit a file through the button it sends the post call but receives a 400 (BAD REQUEST) Error.
It's not even reaching the python from what I can tell, at least not the view code. It seems to form the request URL correctly
http://localhost:8000/QCOMLTE/uploader?qqfile=powered_by.png
And when you go to the URL it sends a message stating that post calls are only allowed.
This is similar to Default django-ajax-uploader with s3 backend gives MalformedXML error
Except that I'm not using any backends, just trying to grab the file/s and save them to a directory.
UPDATE 8/25/14:
Removed the decorator from the view. This results in the error not being sent. After printing the request it becomes evident that the file is being sent to the GET path instead of the FILE path. I don't know how to change this.
After finding this Need a minimal Django file upload example
I preceded to try and imitate it, to find that the FILE and POST requests were actually going through, unlike the ajax/jquery I was using. The JQuery was
$('#uploadform').submit(function (e){
console.log('submitting')
var data = new FormData($('#uploadform').get(0));
$.ajax({
type: 'POST',
url: "{% url 'QCOMLTE:imager' %}",
data: data,
success: function(data){console.log(data)},
error: function(data){console.log(data)},
cache: false,
processData: false,
contentType: false
});
e.preventDefault()
});
except the type was below URL.
I tried changing it to a $.post request and it was trying to post to the wrong URL then...
So I decided to change it back, and this time put type at the top of the ajax call. It then worked... and still does after much testing.
I have a localStorage which i can create an image on the canvas, choose save and it stores the data/url into my localStorage. My question is... how do I send that url or decode it into a blob type to be able to display it in a php page?
I am using ajax and I want to attach it when my formdata is sent. I guess my main issue is trying to figure out the ajax request. Any help with this would be great.
My form sends great but my localstorage item is missing and doesnt get posted to the server.
my ajax
$('#252whole').submit(function(event){
var fd = new FormData( $(this)[0] );
var img = localStorage.getItem('signature');
$.ajax({
type: 'POST',
processData: false,
contentType: false,
async: false,
data: fd,img,
url: 'http://urltomyservice.php',
success: function(data){
alert('Form Submitted...');
},
error: function(){
alert('There was an error converting the pdf!');
}
});
return false;
});
});