admin管理员组文章数量:1332389
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.
本文标签: javascriptWhy is the URL undefined (After JQuery Ajax Post)Stack Overflow
版权声明:本文标题:javascript - Why is the URL undefined (After JQuery Ajax Post) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289305a2447502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论