MVC + controller view not displaying [closed] - ajax

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using ajax for posting 2 values to my controller, the controller got the values but wouldn't display the view. Please assist what I am doing wrong here. Thanks.
Javascript:
function grantPermission() {
window.FB.login(function (response) {
if (response.authResponse) {
uid = response.authResponse.userID;
accesstoken = response.authResponse.accessToken;
var postData = { facebookUID: uid, facebookAccessTok: accesstoken };
$.ajax({
url: '#Url.Action("SavePhoto")',
data: postData,
dataType: 'json',
success: function (response) {
// process the results from the controller action
// window.location.href = response.Url;
}
});
} else {
console.log('User cancelled login or did not fully authorize.');
alert('User cancelled login');
}
}, { scope: 'publish_stream' });
};
Button:
<input type="button" id="auth-loginlink" value="Proceed" onclick="grantPermission();"/>
Controller:
public PartialViewResult SavePhoto(string facebookUID, string facebookAccessTok)
{
return PartialView("BlurredPhoto");
}

You need to embed the returned HTML back into your document somewhere. e.g. If you add a
<div id='MyDiv'>
then change your ajax call to
success: function (responseText, textStatus, XMLHttpRequest) {
$("#MyDiv").html(responseText);
}
As Slaks has mentioned, you also need to change your return datatype accepts to 'html'.

You need to do something with the HTML source from the server in your success callback.
You should probably start by not telling jQuery that the HTML is datatype: 'json'.

Related

How to retrieve data received from AJAX call. Got Undefined

Having tough times trying to deal with data (see markersMethod). Got undefined. All declared logs work fine. In order to clarify the problem, I
provide some code from 3 files related to the ajax call.
This question is NOT a duplicate because the previous was about making ajax call(I figured it out by myself) then it was closed. The actual question deals with handling already made ajax call in the controller side. As you see, these questions are completely different although they share some parts of code.
markers.ejs file
function onMapClick(e) {
coords = e.latlng;
marker = L.marker(coords);
marker.addTo(map);
foo(coords)
}
function foo(coord){
$.ajax({
url:'/markers',
type: 'POST',
dataType: 'json',
data: {coordinates: JSON.stringify(coord)},
success: function(data, text, jqXHR) {
alert(data);
},
error: function(req, status, error) {
alert(status, error);
}
});
}
markersControllers.js file
markersMethod: (req, res) => {
data = req.data;
console.log(req)
console.log('req received');
main.js file
router.post('/markers', markersController.markersMethod)

show message on success ajax [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
On the success function of jquery ajax request, i have to show a div, can anyone suggest a way to do this
dataString = "labid="+val;
jQuery.ajax({
type: "POST",
url: "../pim/manageLabelsAdmin",
cache: false,
data: dataString,
dataType: "json",
success: function(data) {
if(data.success == "yes"){
//alert(data.status);
} else {
alert("Occured internal Error. please check network connection");
}
}
});
If the message to show is in the DOM already, but hidden, you could do something like this in your success callback:
if(data.success == "yes")
{
$("#id-of-message-element").show();
}
Otherwise you could add the message to the DOM using any of .append() or .prepend() or .html() or .text() depending on your needs.
More on the DOM insertion methods here:
http://api.jquery.com/category/manipulation/dom-insertion-inside/
if your div has id then this
$('#yourDivId').show();
else if your fiv has class
$('.yourDivClassName').show();

ajax not working in breezing forms component of joomla [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
function ff_chk_username_unique(element, action)
{
alert(element.value);
alert(action);
var actiona="http://localhost/myproject/check/check_username.php";
var form_data = {
username: element.value,
};
alert(actiona);
$.ajax({
type: "GET",
url: actiona,
data: form_data,
success: function(response)
{
alert(response)
}
});
} // ff_chk_username_unique
code of my ajax file...
file is not called within ajax,
is ajax defined properly?? please help me solve it....
it's working in breezingforms but you need to add jquery file when you use ajax...

How are people handling AJAX posts to ActionMethods using [ValidateAntiForgeryToken]

Seeing that the __RequestVerificationToken is not sent when using AJAX and ValidateAntiForgeryTokenAttribute is looking for the token in Request.Form, how are people dealing with this problem.
I ended up doing this.
$("#regmember-form").submit(function (e) {
e.preventDefault();
var token = $('[name="__RequestVerificationToken"]').val();
alert($(this).attr('action'));
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: { __RequestVerificationToken: token }
});
return false;
});
Very similar to the accepted answer.
I grab the input off the page and send it back with the form post. This assumes that you include it on the page in the first place.
$('#somebutton').click( function() {
var data = $('[name="__RequestVerificationToken"]').serialize();
$.post('/foo/bar', data, function(result) {
// ...
});
});

Jquery Autocomplete not showing results [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have the following code:
var acOptions = {
source:function (request, response) {
$.ajax({
url: "index.php?option=com_fmw&view=keywords_api&controller=keywords_api&format=raw",
type: "GET", dataType: "json",
data: { expr: request.term},
sucess: function (data) {
response($.map(data, function (item) {
return item.value;
}))
}
})
},
minChars: 1,
dataType: 'json'
};
$( "#search_box_input" ).autocomplete(acOptions);
I get the following response from the server:
[{"value":"Greater"},{"value":"great"},{"value":"greatly"},{"value":"Greater-Axe"}]
However, the autocomplete field is not showing results, even though I can see that the ajax request got sent and that the server answered. What am I doing wrong?
sucess is spelt wrong. Try success instead.
success: function (data) {
response($.map(data, function (item) {
return item.value;
}))
}

Resources