Cannot load data into table with ajax - tabulator

I am really having a hard time with upgrading my existing code from 3.5 to 4.0.5.
This time I am facing an error when I try to load data into my table with ajax calls. This did work in 3.5, so I guess something has changed in version 4. I have read the documentation carefully and read the upgrade guide. Has there been changes to the way ajax calls are made such that my code won't work like it did back in 3.5?
At last: I am using the wrapper for jquery.
The table constructor looks like this:
$("#PO-table").tabulator({
...columns etc
ajaxResponse: function (url, params, response) {
//url - the URL of the request
//params - the parameters passed with the request
//response - the JSON object returned in the body of the response.
return response.d; //Return the d Property Of a response json Object
},
});
Then I setData by adding a lot of parameters, as well as the
previously declared ajaxconfig
var ajaxConfig = {
type: "POST", //set request type to Position
contentType: 'application/json; charset=utf-8', //set specific content type
};
$("#PO-table").tabulator("setData", "PurchaseOrder.aspx/Fetch_PurchaseOrders", "{'POnum': '" + ponum + "', 'supplier': '" + supp + "', 'fromDate': '" + from + "', 'toDate': '" + to + "', 'spareNumber': '" + spare + "', 'isDelivered': '" + isdelivered + "', 'isConfirmedOrder': '" + true + "', 'isUnconfirmedOrder': '" + true + "', 'isExactPOnum': '" + false + "', 'isExactSupp': '" + false + "'}", ajaxConfig);
Then I get the error in the console and the data won't load into the table:

By default Tabulator will send data in POST requests as form data, if you want to send it as JSON you will need to use the ajaxContentType option
var table = new Tabulator("#example-table", {
ajaxURL:"http://www.getmydata.com/now", //ajax URL
ajaxConfig:"POST", //ajax HTTP request type
ajaxContentType:"json", // send parameters to the server as a JSON encoded string
});
This will encode the data as a JSON object and set the appropriate headers.
The ajaxContentType option was added in version 4.1 to make it easier for users to send requests back to their servers with a variety of content types

Related

What is REST API end point for deleting specific version of a SharePoint Document

I am trying to delete specific version of SharePoint document by Id.
I have retrieved all the SharePoint Document versions by REST call with below code.
let URL : string = `${this.context.pageContext.web.absoluteUrl}/_api/Web/lists/getById('${listId}')/items(${documentId.Id})/versions`;
this.context.spHttpClient.get(URL,SPHttpClient.configurations.v1).then(response=>{
return response.json();
}).then(json=>{
return json.value;
})
What endpoint should I call to delete specific version by it's version Id?
The endpoint for deleting the specific version of a file will like this:
https://Tenant.sharepoint.com/sites/SiteName/_api/web/GetFileByServerRelativeUrl('/sites/SiteName/DocName/code.png')/versions/DeleteByLabel('1.0')
Need to use GetFileByServerRelativeUrl to get the file object and then use DeleteByLabel for deleting the specific version, here is a complete code demo for your reference:
function DeleteFileVersionByVersionLabel() {
var WebServerRelativeUrl = _spPageContextInfo.webServerRelativeUrl;
// Provide Internal name of the library here
var DocuentLibraryInternalName = "Document%20Library";
// Provide name of the document
var DocumentName = "test doc.docx";
var ServerRelativeUrlofFile = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('" + WebServerRelativeUrl + "/" + DocuentLibraryInternalName + "/" + DocumentName + "')"
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
// NOTE: Version Label is nothing but the version number you see in the Version History
url: ServerRelativeUrlofFile + "/versions/DeleteByLabel('4.5')",
type: "POST",
headers:
{
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// IF-MATCH header: Provides a way to verify that the object being changed has not been changed since it was last retrieved.
// "IF-MATCH":"*", will overwrite any modification in the object, since it was last retrieved.
"IF-MATCH": "*",
//X-HTTP-Method: The MERGE method updates only the properties of the entity , while the PUT method replaces the existing entity with a new one that you supply in the body of the POST
"X-HTTP-Method": "DELETE",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log("Success");
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
}
Reference:
Delete File Version By Version Label in SharePoint using REST API

How to transfer params to GET method in Express?

I have the following URL:
'http://localhost/track/' + id + '/fromdate/' + date_start + '/todate/' + date_end
And there is handler in Node.js part:
app.get('/track/:id/fromdate/:fromdate/todate/:todate', function(req, res){
alert(req.params.fromdate);
});
Why I can not get req.params.fromdate
can you try this please:
'http://localhost/track/' + encodeURIComponent(id) + '/fromdate/' + encodeURIComponent(date_start)+ '/todate/' + encodeURIComponent(date_end)
when you are making a get request, you should turn the parameters into encoded URI components like in the example i give

Node Express - Http post to return a file

I'm not able to return a file, when submitting a HTTP post
I know that all of my code is functioning, up until the download / sendfile part.
var postData = req.body;
var outputString = "";
mkdirp('./server/tmp');
_.each(postData, function (each) {
outputString += each.variable + ": " + each.value + " !default;\n";
});
fs.writeFile(path.resolve("./server/tmp/ionic.app.scss"), outputString);
res.attachment(path.resolve("./server/tmp/ionic.app.scss"));
res.end('hello,world\nkeesun,hi', 'UTF-8');
How can I make it so that, when the user clicks a button, it sends a POST request, which in turn downloads a file that is created on the fly by the node server?
res.attachment() only sets the content disposition header. Use res.download() instead.
res.download(path.resolve("./server/tmp/ionic.app.scss"));
res.download() both sets the content disposition header and sends the file.
-- EDIT --
Like I mentioned in my last comment, I failed to mentioned that you should remove the final res.end() in your code.
I'm wondering, though, why you're saving the generated text to a file just to send it. A simplified way of doing it would be as follows:
var postData = req.body;
var outputString = "";
mkdirp('./server/tmp');
_.each(postData, function (each) {
outputString += each.variable + ": " + each.value + " !default;\n";
});
res.attachment("ionic.app.scss");
res.send(outputString);
res.end();
This will accomplish the same goal, without having to save the outputString to a file first.

Nodejs net send json?

I am trying to send json formated data over a net socket in nodejs like this:
socket.write("{\"id\":\"1\", \"type\":message, \"msg\": " + obj.msg + ", \"name\": " + obj.msg + ", \"time\": " + getDateTime() + "}", socket);
For some reason this does not work, any ideas what i am doing wrong?
There are few node.js modules that can help:
json-over-tcp: https://www.npmjs.org/package/json-over-tcp
json-socket: https://www.npmjs.org/package/json-socket
Simple way is to use the JSON object (ECMAScript 5) with simply converting any object to string using JSON.stringify(). Use the returned string to send over the socket.
socket.write(JSON.Stringify({
data: "stuff"
});
Then use JSON.Parse on the other side. This is how I'm doing it.

POST data received in node.js server returned to client as undefined

I'm starting to learn node.js. I'm following this tutorial and I've run into a problem where some POST data is received properly but when it's returned to the client, it becomes "undefined".
Here's the code for grabbing the POST data (this is Coffeescript btw):
postData = ""
request.setEncoding "utf8"
request.addListener "data", (postDataChunk) ->
postData += postDataChunk
console.log "Received POST data chunk '" + postDataChunk + "'."
request.addListener "end", ->
console.log "postData at end: " + postData
POST = qs.parse postData
console.log POST
route handle, pathname, response, POST.text
The POST text is sent to a routing function along with the response object. The code there is:
upload = (response, postData) ->
console.log "Request handler 'upload' was called"
console.log "post data in upload: " + postData
response.writeHead 200, "Content-Type": "text/plain"
response.end "You sent: " + postData
In the console output, PostData is set correctly but when I view the output in the browser it'll always say "You sent: undefined"
Can anyone help me understand what's going wrong?
You need to look at POST.text inside the end callback. Instead of console.log POST, do console.dir POST and see if the POST object has a property named text defined. My guess is it does not. If not, log the raw postData string and see if it is not what you expect.

Resources