Previously when I visited /cart/clear or did the below, it cleared my cart, and it's attributes. Now when I do either of those things, it clears the products, but the cart attributes are persisting. I'm not sure if this is a recent change or a Shopify bug?
Any idea how to clear the cart's attributes?
$.ajax({
type: "POST",
url: '/cart/clear.js',
success: function(){
alert('You cleared the cart!');
},
dataType: 'json'
});
I've found a way to delete them all by doing this, which seems overly complicated, but it works.
$.ajax({
type: "GET",
url: '/cart.js',
dataType: 'json',
success: function(resp) {
var nullHash = {};
for ([key, value] of Object.entries(resp.attributes)) {
nullHash[key] = null;
}
$.ajax({
type: "POST",
url: '/cart/update.js',
data: {attributes:nullHash},
dataType: 'json'
});
}
});
Or if you want to delete them by name, you can do this:
var nullHash = {
'someAtt1': null,
'someAtt2': null,
'someAtt3': null
};
$.ajax({
type: "POST",
url: '/cart/update.js',
data: {attributes:nullHash},
dataType: 'json'
});
Related
I've got an odd thing happening. On my index page I've got an ajax call that grabs a parameter off of an SVG and then hits the following code:
public async Task<IActionResult> OnPostStateClickAsync(string state)
{
HttpContext.Session.SetObject("CurrentFilters", SearchFilters);
SearchFilters.State = state;
return RedirectToPage("/Search");
}
I'm able to attach a debugger and see everything is working fine behind the scenes. The problem, the UI doesn't ever update and take me to the Search page. If I manually navigate there all the data is loaded and everything is fine.
Not seen this behavior before and I've used the same code in previous projects.
Here is the AJAX call in case it's relevant:
$(document).ready(function () {
$('path').click(function () {
var stateParameter = $(this).attr('id');
$.ajax({
type: "POST",
url: '/Index?handler=StateClick&state=' + stateParameter,
//data: searchTitle,
contentType: "application/json; charset=utf-8",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
dataType: "json"
}).done(function (data) {
console.log(data.result);
location.reload();
});
});
});
You can change your code like bellow.
Action:
public async Task<IActionResult> OnPostStateClickAsync(string state)
{
//...
return new JsonResult(new { redirectToUrl = "/Search" });
}
In your Ajax:
$(document).ready(function () {
$('path').click(function () {
var stateParameter = $(this).attr('id');
$.ajax({
type: "POST",
url: '/Index?handler=StateClick&state=' + stateParameter,
//data: searchTitle,
contentType: "application/json; charset=utf-8",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
dataType: "json",
success: function (response) {
window.location.href = response.redirectToUrl;
}
});
});
});
Test Result:
I have try to upload a video to vimeo account directly from browser using api ,video details are created but file seems to corrupted/upload not happens.This is my sample code.
var file = $(this).prop("files")[0];
var formData = new FormData();
formData.append("file_data", file);
$.ajax({
url: "https://api.vimeo.com/me/videos/",
type: "post",
data: formData,
headers: {
"Authorization": "Bearer -----",
},
processData: false,
mimeType: "multipart/form-data",
contentType: false,
}).done(function (response) {
// Do something
}).complete(function (response) {
// Do something
}).fail(function (e) {
// Do something
});
vimeo video listing shows blank thumbnail
enter image description here
Try this piece of code. I have made some changes here:
var file = $(this).prop("files")[0];
var formData = new FormData();
formData.append("file_data", file);
$.ajax("https://api.vimeo.com/me/videos/", {
type: "POST",
headers: {
"Authorization": "Bearer -----",
},
data: formData,
contentType: "multipart/form-data", // changed this
dataType: "json",
crossDomain: true // for CORS policy error
}).done((response) => {
// Do something
}).fail((error) => {
// Do something
}).complete(() => {
// Do something
});
I have chaged contentType and removed mimeType. I've also removed un-necessary processData field.
I am using Fremework7 v1.4.0 and there is no jsonpCallback or jsonp and other related JSONP parameters in the documentation. Only crossDomain: true is available out there.
Is Framework7 (Dom7) supports JSONP and callback parameters like jQuery?
I've used the code below without success:
var $$ = Dom7;
$$.ajax({
url: "http://query.yahooapis.com/v1/public/yql",
contentType: "OPTIONS",
crossDomain: true,
data: {
q: "select title,abstract,url from search.news where query=\"cat\"",
format: "json"
},
success: function( response ) {
alert( 'ok' );
alert( response );
}
});
I don't know this way is the best way or not, I had the same problem and this way works for me.
var $$ = Dom7;
$$.ajax({
url: "http://query.yahooapis.com/v1/public/yql",
contentType: "OPTIONS",
dataType : 'json',
crossDomain: true,
data: {
q: "select title,abstract,url from search.news where query=\"cat\"",
format: "json",
callback:function(){
return true;
}
},
success: function( response ) {
alert( 'ok' );
alert( response );
}
});
I am new to MVC and I am trying to create an application with Knockout.js to send data back to the server dynamically. I am following an example i found at:
http://www.mytecbits.com/microsoft/dot-net/knockout-js-and-bootstrap-with-asp-net-mvc-part-2
It works perfectly off the site, but i am trying to send data to multiple models instead just one as in the example
The Knockout code used in the example to send the data back to the server is
var urlPath = window.location.pathname;
var CreateArticleVM = {
Title: ko.observable(),
Excerpts: ko.observable(),
Content: ko.observable(),
Test: ko.observable(),
btnCreateArticle: function() {
$.ajax({
url: urlPath + '/Create',
type: 'post',
dataType: 'json',
data: ko.toJSON(this),
contentType: 'application/json',
success: function(result) {
window.location.href = urlPath + '/';
},
error: function(err) {
if (err.responseText == "success") {
window.location.href = urlPath + '/';
}
else {
alert(err.responseText);
}
},
complete: function() {}
});
}
};
ko.applyBindings(CreateArticleVM);
How do i modify the above code to be able to accept a FormCollection? Or what is the best solution to my problem?
Thanks
Say your service is expecting more than one argument like following.
[HttpPost]
public String Create(ModelA modela, ModelB modelb)
{
//Server code.
}
In order pass the data for Create method from client side you need to form your postdata as follows.
$.ajax({
url: urlPath + '/Create',
type: 'post',
dataType: 'json',
data: {modela: { "modela data in the expected form" }, modelb : { "modelb data.." } },
contentType: 'application/json',
success: function(result) {
window.location.href = urlPath + '/';
},
.
.
.
.
});
Below is my jquery codes, and it always return internal server error, and i'm sure that my wcf service can be called from i.e and iis.
$.support.cors = true;
$.ajax({
type: "post",
url:"http://localhost:8080/WcfService/FunHeartWork.svc/UpdateMedia",
data: '{"desc":"' + text + '","albumId":"'+albumId+'","mediaId":"'+mediaId+'","userName":"'+userName+'"}',
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
},
error:function(a,b,c){alert(c);},
failure: function (msg) {
alert(msg);
}
});
Try changing the data line to:
data: {
desc: text,
albumId: albumId,
mediaId: mediaId,
userName: userName
},