So basically, I'm trying to refresh the view after I do an Ajax Post in a Profile View.
$.ajax({
url: '/Profile/Index',
dataType: "html",
type: "POST",
data: JSON.stringify(10),
success: function(returl) {
alert('It worked');
window.location.href = returl.url;
},
error: function(jqXHR,responseText,textStatus) {
alert(jqXHR.responseText)
}
});
This is the HttpPost Action:
[HttpPost]
public ActionResult Index(string number){
//Things to do
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Profile");
return Json(new { Url = redirectUrl });
}
And after the post is made I get this URL: http://localhost:50738/undefined I already debugged the Controller method and it gets /Profile correctly. I can't understand why this problem keeps going... Thanks!
So basically, I'm trying to refresh the view after I do an Ajax Post in a Profile View.
$.ajax({
url: '/Profile/Index',
dataType: "html",
type: "POST",
data: JSON.stringify(10),
success: function(returl) {
alert('It worked');
window.location.href = returl.url;
},
error: function(jqXHR,responseText,textStatus) {
alert(jqXHR.responseText)
}
});
This is the HttpPost Action:
[HttpPost]
public ActionResult Index(string number){
//Things to do
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Profile");
return Json(new { Url = redirectUrl });
}
And after the post is made I get this URL: http://localhost:50738/undefined I already debugged the Controller method and it gets /Profile correctly. I can't understand why this problem keeps going... Thanks!
Share Improve this question edited Jul 24, 2015 at 14:21 Holt 37.7k7 gold badges98 silver badges143 bronze badges asked Jul 24, 2015 at 14:13 Cajux94Cajux94 1271 gold badge3 silver badges15 bronze badges 1-
What does
console.log(returl)
print? – galactocalypse Commented Jul 24, 2015 at 14:20
3 Answers
Reset to default 3You're telling the Ajax request to expect HTML to be returned and not JSON, so returl will quite literally be a string value and thus returl.url will be undefined. Change your datatype to json instead.
Found this on http://api.jquery./jquery.ajax/#jQuery-ajax-settings. Look at the datatype parameter for more info.
$.ajax({
url: '/Profile/Index',
dataType: "json",
type: "POST",
data: JSON.stringify(10),
success: function (returl) {
alert('It worked');
window.location.href = returl.Url;
},
error: function (jqXHR, responseText, textStatus) {
alert(jqXHR.responseText);
}
});
If you are returning just a single string, return it as it is
Controller Code
return Json(redirectUrl);
and directly use the string in response
$.ajax({
url: '/Profile/Index',
dataType: "json",
type: "POST",
data: JSON.stringify(10),
success: function (returl) {
alert('It worked');
window.location.href = returl;
},
error: function (jqXHR, responseText, textStatus) {
alert(jqXHR.responseText);
}
});
Try location.href=returl.Url
instead of location.href=returl.url
. Also fix the expected type to 'json' as the above answer points out.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742289305a4415895.html
评论列表(0条)