SharePoint Document's Library metadata duplicates after REST call - ajax

I am making a POST ajax request to a SharePoint Document Library folder to save a file along with some metadata. The file and the metadata are saved correctly however, the metadata are sometimes(not all the time) replicated inside the document library(where I place folder not files). Why does this happen? How can I prevent this from happening? Here is a code snippet of the ajax call:
var result = $.ajax(
{
url: siteUrl.concat("/lists/getbytitle('")
.concat("Document Library Name")
.concat("')/RootFolder/folders('")
.concat("Folder Name")
.concat("')/files/add(url = '").concat(fileName).concat("', overwrite = true)"),
type: "POST",
data: arrayBuffer,
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $('#__REQUESTDIGEST').val()
},
contentType: "application/json;odata=verbose",
processData: false,
success: function (data) {
},
error: function (data) {
console.log(data);
},
async: false
});

Related

Modify xml using AJAX

I have the following xml file:
<User>
<Address>123</Address>
<FirstName>John</FirstName>
<SecondName>Smith</FirstName>
</User>
I am trying to get it changed with an AJAX call:
var addressid= = 123;
$.ajax({
type: "GET",
url: url,
headers: {
"Authorization": "Basic " + btoa("username:password"),
},
contentType: "application/xml",
dataType: "xml",
async: true,
crossDomain: true,
success: function(xmlResponse) {
alert("Your user has been edited");
$(xmlResponse).find('User').each(function(){
if($(this).find('Address').text() == addressid) {
$(this).find('FirstName').text("Jenifer");
$(this).find('SecondName').text(secondName);
}
});
}
});
An alert comes, this does not display any errors, but it doesn't get passed on to the system. Which means, if I fetch the data from the server, the old name gets displayed. What am I doing wrong?

django ajax MultiValueDictKeyError

I am receiving this error:
MultiValueDictKeyError at /orders/ajax/add_order_line
"'cart'"
Here is my script
var cart = {
0: {
id: "1",
quantity: 50
}
}
$.ajax({
url: myURL,
type: "post",
data: {cart: cart},
success: function() {},
error: function(){}
});
Meanwhile in my django views, the error was found in this line:
def something(request):
cart = request.POST['cart']
Use get method of multivaluedict
request.POST.get('cart')
Your data is a nested array, so you can't send it using the default default application/x-www-form-urlencoded content type.
You can send the data as json:
$.ajax({
url: myURL,
type: "post",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({cart: cart}),
success: function() {},
error: function(){}
});
Then in your view, you have to load the json string from request.body instead of using request.POST (which is for form-encoded data only).
import json
def my_view(reqest):
data = json.loads(request.body.decode('utf-8'))
cart = data.get('cart')

Adding Attachment file to an external list item using AJAX throws Internal Server Error

i'm working with Sharepoint-hosted apps with OData. I am trying to add attachment file to a list item after the item is created using its BdcIdentity but i am getting the error below:
{"readyState":4,"responseText":"{"error":{"code":"-2146232832,Microsoft.SharePoint.SPException","message":{"lang\":"en-US","value":"The List item must be saved to the content database before adding an attachment. To resolve this problem, save the list item and then add the attachment."}}}","status":500,"statusText":"Internal Server Error"}
Code to create the list item:
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('tbl_FeedbackDetails')/Items/",
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose"
},
data: JSON.stringify(details),
success.....
Below is my code after the item is created (im running this code inside the success function of the fist ajax call):
var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/GetByTitle('ListName')/GetItemByStringId('BdcIdentity')/AttachmentFiles/add(FileName='filename')";
$.ajax({
url: queryUrl,
type: "POST",
processData: false,
contentType: "application/json;odata=verbose",
data: buffer,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
console.log(JSON.stringify(data));
console.log('Attachement Added Successfully');
},
error: function (error) {
console.log("Failure:" + error.status + "," + error.statusText);
console.log('Error full-text: ' + JSON.stringify(error));
}
});
The first call was successful in creating the list item. Am I doing things right?

Ajax Success Function not working

I am using Ajax to add contents on my database. And here's the code:
function addToFavorites(){
var recipe_id = $("#recipe_id").val();
var url = connect_url+'addFavorite.php?user_id='+user_id+'&recipe_id='+recipe_id;
$.ajax({
type: 'POST',
data: [{
user_id: user_id,
recipe_id: recipe_id
}],
url: url,
async: true,
jsonpCallback: 'userCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function (data) {
alert("HELLO!!!");
},
error: function (e) {
alert("ERROR!");
}
});
}
The Ajax call was successful and I was able to add records on the database but I'm just wondering why is it not displaying the alert message if the calling was successful? Is there something wrong with my code? Or is there something wrong with my understanding? Thanks!
you must give a response with some info to the ajax or it won't know the response succeeded

$.ajax is not working

In my web page there is a textbox to get the scanned barcode value. Once we scan the barcode it has to get details from the database. I am creating the change event for the textbox.
Problem: $.ajax is not working.
Code:
var target = $('#txtBarcode'), val = target.val();
target.change(monitor());
function monitor() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
url: "HomePage.aspx/SearchProduct",
dataType: "json",
success: function(data) {
alert("Success!!!");
}
});
}
You are trying to pass 'monitor' to the change method but you're actually calling it. It should look like this (no parens)
var target = $('#txtBarcode'), val = target.val();
target.change(monitor);
function monitor() {
You can always declare it inline too:
var target = $('#txtBarcode'), val = target.val();
target.change(
function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
url: "HomePage.aspx/SearchProduct",
dataType: "json",
success: function(data) {
alert("Success!!!");
}
});
});
Add an error handler.
Make sure your relative URL is right.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
url: "HomePage.aspx/SearchProduct",
dataType: "json",
success: function(data) {
alert("Success!!!");
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
// ...
}
});
EDIT: Dan is right about your change handler.
You can copy some answers posted here, and at least one of will likely to work, but you won't get the intimate knowledge of why. Here's an additional way:
Since you use asp.net, put the break point in the first line of HomePage.aspx/SearchProduct. This ensure that the request goes to the right URL on the server.
Step all the way through this method to make sure there's no exception that gets thrown.
Use FireFox and install Firebug (even if you target IE and have no intention to make it run on FF). You can inspect the http response.
Add an error handler in addition to the success handler.

Resources