How do I use put / patch / delete method in jQuery AJAX? - web-deployment

I want to use patch method in my laravel 9 application.
I used jquery ajax to update data.
I used " type:"PATCH" " but it shows me 419 method not allow
Laravel 9.
Route:
Route::patch('admin/contact/update/process',[contactController::class,'edit'])->name('contactEdit');
AJAX:
function update(rowId,mid,token)
{
let tableObject=document.getElementById('customtable');
let name=tableObject.rows[rowId].cells[0].innerHTML;
let profilelink=tableObject.rows[rowId].cells[1].innerHTML;
let imagelink=tableObject.rows[rowId].cells[3].innerHTML;
let alt=tableObject.rows[rowId].cells[4].innerHTML;
//alert(imagelink);
$.ajax({
url:"./process",
type:'PATCH',
data:{
'mid':mid,
'name':name,
'profilelink':profilelink,
'imagelink':imagelink,
'alt':alt,
'_token':token,
'_method': 'PATCH'
},
success:function(respons){ if(parseInt(respons)==1){window.location.reload();/*alert(respons);*/}else{ alert("something wrong");}
},
error:function(Xhr, textStatus, errorMessage){
console.log("xhr.status:"+Xhr.status);
alert("xhr.status:"+Xhr.status);
alert("textstatus: "+textStatus);
alert("error Message:"+errorMessage);
console.log("error Message:"+errorMessage);
}
});
}
This is not working.
console shows:
PATCH http://localhost/ismaildev/public/admin/contact/update/process 419 (unknown status)
Anyone please write me some hints solution or provide me solution link how to use patch / put / delete method in jQuery AJAX?

Related

Upload file size limit with custom webscript on Alfresco Community 5.2

I need help to upload document content back in Alfresco Community 5.2 though a share javascript.
The destination noderef is already existing, I upload a new version of a document.
I can't use the api/upload web service because I also need to do some operation on the noderef and I have a base64 content of the file which need to be converted.
So I wrote a new webscript and it is all working fine, at least while I upload documents which are smaller than 3MB,
Here it is the code
Alfresco.util.Ajax.request({
method: Alfresco.util.Ajax.POST,
dataObj: {
bytes: response.bytes,
digestAlgorithm: response.digestAlgorithm,
mimeType: response.mimeType.mimeTypeString,
name: response.name,
nodeRef: this.nodeRef,
signatureLevel: this.signatureLevel
},
url: thisClass.urlAlfrescoService + "myOrg/myPackage/uploadDocument",
successCallback: {
fn: thisClass._successOnUploadContent,
scope: this
},
failureCallback: {
fn: thisClass._errorOnUploadContent,
scope: this
},
scope: this,
noReloadOnAuthFailure: true
});
Do I miss some option to increase max upload file size?
I tryed uploading the file normally (with drag and drop) and it works.
The problem is when the file is >= 3MB the java class behind the webscript does not receive any byte
UPDATE
After some researches I found it could be a problem of how data are passed through POST, as application/x-www-form-urlencoded instead of multipart/form-data, but I can't find a way to specify the request content type in the ajax request
SOLUTION
The problem was the application/x-www-form-urlencoded instead of the multipart/form-data, I used a fetch POST request as stated here, but also the ajax request solution is good.
Last week,I had a same very similar problem with Alfresco AJAX request on Alfresco 5.0.2.5 and I used jquery's AJAX calls and it worked for me.
$.ajax({
url: Alfresco.constants.PROXY_URI + "your_web_script",
type: "POST",
data: dataFromFiles,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
dataType: "text",
success: function(data, textStatus, jqXHR) {
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
Reference link : https://blog.arvixe.com/sending-multipart-form-using-ajax/
Hope this helps you.

How to call external rest api using metero HTTP module

I am currently new to Meteor and Angular2 and I built an application with same.
I want to make External API call inside Meteor Server using Meteor HTTP Module.
Could you please give an example code in "Typescript" how to do this? Thanks in advance.
Use HTTP.call() for external invoking API. You can call both Get and post requests with this. See the documentation link for details.
Here is a simple example
HTTP.call('POST', 'http://api.twitter.com/xyz', {
data: { some: 'json', stuff: 1 }
}, () => (error, result) {
if (!error) {
Session.set('twizzled', true);
}
});

Meteor audit-argument-checks

I have a Meteor project where I'm using audit-argument-check. I'm getting an error message
Error: Did not check() all arguments during publisher 'document
I'm know this is related the audit-argument-check not being able to check all arguments. But as far as I'm concerned, I checked all of them. Concrete, I have defined a collection 'documents' and attached a SimpleSchema. As part of iron-router, I have the following:
Router.route('customerDocumentShow', {
template: 'customerDocumentShow',
path: 'customer/documents/:_id',
waitOn: function () {
return Meteor.subscribe('document', this.params._id);
},
data: function () {
return Documents.findOne(this.params._id);
}
});
So I'm passing only the documentId (this.params._id). On the server, I have defined a method:
Meteor.methods({
documentsReadMethod: function(documentId){
check(documentId, String);
var documentItem = Document.findOne(argument);
if (!documentItem) {
throw new Meteor.Error(500, 'Error 500: Not Found', 'No documents found.');
}
return documentItem;
}
});
So I'm checking to documentId in the server method. So not sure why I'm getting this error message.
Note: One thing I'm not entirely sure about though is how I need to call this method (right now, it's documentsReadMethod_. I'm not explicitly calling (on the client):
Meteor.call(documentsReadMethod, this.params_id);
as I'm using autoform, collection2 and simpleschema. I've been spending the entire weekend, but have no clue. Any idea's ?
Note: the code is on github: https://github.com/wymedia/meteor-starter-base
The problem is in the publish. You didn't check the id here:
https://github.com/wymedia/meteor-starter-base/blob/master/server/publications/documents.js#L16
Just add check(id, String); line 16 and it should work.
I have the same problem with another tuto !
Answer found at check is not defined in meteor.js : since Meteor v1.2, you have to add this package:
$ meteor add check

Implementing a simple search with Meteor and Iron Router

In the next phase of my Meteor journey (read: learning the ropes!), I'd like to implement a simple search based on user inputed values, then redirect to a route specific to the record returned from the server.
At the moment, I'm picking up the inputed values via this code:
Template.home.events 'submit form': (event, template) ->
event.preventDefault()
console.log 'form submitted!'
countryFirst = event.target.firstCountrySearch.value
countrySecond = event.target.secondCountrySearch.value
Session.set 'countryPairSearchInputs', [countryFirst, countrySecond]
countryPairSearchInputs = Session.get 'countryPairSearchInputs'
console.log(countryPairSearchInputs)
return Router.go('explore')
Happily, the console log returns the desired countryPairSearchInputs variable - an array of two ids. In my routes.coffee file I then have the following:
#route "explore",
path: "/explore/:_id"
waitOn: ->
Meteor.subscribe 'countryPairsSearch'
On the server side, I have:
Meteor.publish 'countryPairsSearch', getCountryPairsSearch
And finally, I have a search.coffee file in my /lib directory that defines the getCountryPairsSearch function:
#getCountryPairsSearch = ->
CountryPairs.findOne $and: [
{ country_a_id: $in: Session.get('countryPairSearchInputs') }
{ country_b_id: $in: Session.get('countryPairSearchInputs') }
]
With regards to the search function itself, I have a CountryPairs collection where each record has two ids (country_a_id and country_b_id) - the aim here is to allow users to input two countries, with the corresponding CountryPair then being returning.
I'm currently struggling to tie all the pieces together - the console output on searching is currently:
Uncaught Error: Missing required parameters on path "/explore/:_id". The missing params are: ["_id"]. The params object passed in was: undefined.
Any help would be greatly appreciated - as you can probably tell I'm new to Meteor and am still getting used to the pub/sub methodology!
Edited: mixed up client/server for the publish method when I first posted - the danger of late-night posting!
First, seems you're expecting an :id parameter on your 'explore' route.
If I understand you're case, you're not expecting any params here, so you can just delete ':id' from your route:
#route "explore",
path: "/explore/"
waitOn: ->
Meteor.subscribe 'countryPairsSearch'
or either add a params to your Router.go call:
Router.go('explore', {_id: yourIdVar});
Secondly, you're trying to use a client function: Session.get() server-side. Try to update the publication using a parameter ; or using a method.call.
client-side
Meteor.subscribe 'countryPairsSearch' countryA countryB
not sure about the coffeescript syntax, check http://docs.meteor.com/#/full/meteor_subscribe
and server-side
#getCountryPairsSearch = (countryA, countryB) ->
CountryPairs.findOne $and: [
{ country_a_id: $in: countryA }
{ country_b_id: $in: countryB }
]

How can I implement a call to a package that doesn't use fibers from with Meteor?

I am trying to use twit in Meteor in order to communicate to the Twitter REST api.
It works fine in say a server.js file in the /server/ directory if I call it by itself. If I wrap or call it from within say an observe or even call a function that calls twit's functions from an observe I get errors.
For example this works perfectly fine within a /server/server.js.
T.post('statuses/update', { status: 'hello world!' }, function(err, reply) {
console.log('error: ' + JSON.stringify(err,0,4));
console.log('reply: ' + JSON.stringify(reply,0,4));
});
But, suppose I want to say call Twitter every time a record is inserted.
var query = Posts.find({}, {fields: {}});
var handle = query.observe({
added: function(post, before_index){
if(post.twitter_id_str === undefined || post.twitter_id_str === '' ||
post.twitter_id_str === null) {
T.post('statuses/update', { status: 'hello world!' }, function(err, reply) {
console.log('error: ' + JSON.stringify(err,0,4));
console.log('reply: ' + JSON.stringify(reply,0,4));
if(reply){
// TODO update record with twitter id_str
// BREAKS here - crash, restart
console.log('incoming twitter string: ' + reply.id_str);
Posts.update(
{_id: post._id},
{$set:{twitter_id_str:reply.id_str}}
);
}
});
} else {
console.log('remove me we have it: ' + post.twitter_id_str);
}
}
});
Which throws this error, server crashes and restarts but no code logic is run where I have commented the Break.
app/packages/mongo-livedata/collection.js:215
throw e;
^
Error: Meteor code must always run within a Fiber
at [object Object].get (app/packages/meteor/dynamics_nodejs.js:14:15)
at [object Object]._maybeBeginWrite (app/packages/mongo-livedata/mongo_driver.js:68:41)
at [object Object].update (app/packages/mongo-livedata/mongo_driver.js:191:20)
at [object Object].update (app/packages/mongo-livedata/collection.js:203:32)
at app/server/server.js:39:13
at /usr/lib/meteor/lib/node_modules/twit/lib/oarequest.js:85:16
at passBackControl (/usr/lib/meteor/lib/node_modules/twit/node_modules/oauth/lib/oauth.js:359:11)
at IncomingMessage.<anonymous> (/usr/lib/meteor/lib/node_modules/twit/node_modules/oauth/lib/oauth.js:378:9)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.onMessageComplete (http.js:137:23)
Exited with code: 1
In summary, the Twitter code runs fine on it's own but not when within the Meteor fibers stuff. I tried putting it in another function and calling that from within the observe etc... no avail.
Any recommendations or ideas?
You'll need to do the twit post API call in a fiber:
Fiber(function() { ... your twit API call ... }).run()
Have a look at this related question: "Meteor code must always run within a Fiber" when calling Collection.insert on server

Resources