admin管理员组文章数量:1130349
This is my controller,
public ActionResult ReturnMethodTest(int id)
{
string name = "John";
return Json( new {data=name});
}
I am trying to get data from this controller by using code below but I am getting .
Can you please tell me what am I doing wrong?
$.ajax({
url: @Url.Action("ReturnMethodTest", "HomeController"),
data: {
id: 5,
},
success: function (data) {
console.log(data);
}
});
This is my controller,
public ActionResult ReturnMethodTest(int id)
{
string name = "John";
return Json( new {data=name});
}
I am trying to get data from this controller by using code below but I am getting .
Can you please tell me what am I doing wrong?
$.ajax({
url: @Url.Action("ReturnMethodTest", "HomeController"),
data: {
id: 5,
},
success: function (data) {
console.log(data);
}
});
Share
Improve this question
edited May 19, 2016 at 2:37
Michael Gaskill
8,04210 gold badges39 silver badges44 bronze badges
asked Jul 13, 2015 at 6:54
Da ArtagnanDa Artagnan
1,1994 gold badges18 silver badges28 bronze badges
2
|
3 Answers
Reset to default 53@Url.Action only returns the action url's string, without quotes around it.
You'll need to wrap that url in quotes.
Replace:
url: @Url.Action("ReturnMethodTest", "HomeController"),
With:
url: '@Url.Action("ReturnMethodTest", "HomeController")',
// ^ ^
Otherwise, the file returned to the client will contain:
url: /HomeController/ReturnMethodTest,
Which isn't valid JS, nor what you want. The replacement gives the following result:
url: '/HomeController/ReturnMethodTest',
Which is a perfectly valid JavaScript string.
A Javascript regular expression literal looks like this -- a pattern enclosed between slashes:
var re = /pattern/flags;
If you interpolate a variable that begins with a slash but do not put quotation marks around it, it will be interpreted as a regular expression, not a string. Another time this comes up is with JSP expression language, where you should write the first, not the second:
var spinner = "<img src='${contextPath}/images/ajax-loader-small.gif'>"
var spinner = "<img src='" + ${contextPath} + "/images/ajax-loader-small.gif'>"
Please remove the Controller suffix while specifying in url.
Try it this way:
url: '@Url.Action("ReturnMethodTest", "Home")'
This is my controller,
public ActionResult ReturnMethodTest(int id)
{
string name = "John";
return Json( new {data=name});
}
I am trying to get data from this controller by using code below but I am getting .
Can you please tell me what am I doing wrong?
$.ajax({
url: @Url.Action("ReturnMethodTest", "HomeController"),
data: {
id: 5,
},
success: function (data) {
console.log(data);
}
});
This is my controller,
public ActionResult ReturnMethodTest(int id)
{
string name = "John";
return Json( new {data=name});
}
I am trying to get data from this controller by using code below but I am getting .
Can you please tell me what am I doing wrong?
$.ajax({
url: @Url.Action("ReturnMethodTest", "HomeController"),
data: {
id: 5,
},
success: function (data) {
console.log(data);
}
});
Share
Improve this question
edited May 19, 2016 at 2:37
Michael Gaskill
8,04210 gold badges39 silver badges44 bronze badges
asked Jul 13, 2015 at 6:54
Da ArtagnanDa Artagnan
1,1994 gold badges18 silver badges28 bronze badges
2
-
1
try this:
url: '@Url.Action("ReturnMethodTest", "HomeController")'– Umesh Sehta Commented Jul 13, 2015 at 6:55 - My Code looks like this $.ajax({ url: '@Url.Action("ReturnMethodTest", "Home")', data: { id: 5, }, success: function (data) { console.log(data); } }); And firebug throws "500 internal Server Error" /i59.tinypic.com/5y96ok.png – Da Artagnan Commented Jul 13, 2015 at 7:13
3 Answers
Reset to default 53@Url.Action only returns the action url's string, without quotes around it.
You'll need to wrap that url in quotes.
Replace:
url: @Url.Action("ReturnMethodTest", "HomeController"),
With:
url: '@Url.Action("ReturnMethodTest", "HomeController")',
// ^ ^
Otherwise, the file returned to the client will contain:
url: /HomeController/ReturnMethodTest,
Which isn't valid JS, nor what you want. The replacement gives the following result:
url: '/HomeController/ReturnMethodTest',
Which is a perfectly valid JavaScript string.
A Javascript regular expression literal looks like this -- a pattern enclosed between slashes:
var re = /pattern/flags;
If you interpolate a variable that begins with a slash but do not put quotation marks around it, it will be interpreted as a regular expression, not a string. Another time this comes up is with JSP expression language, where you should write the first, not the second:
var spinner = "<img src='${contextPath}/images/ajax-loader-small.gif'>"
var spinner = "<img src='" + ${contextPath} + "/images/ajax-loader-small.gif'>"
Please remove the Controller suffix while specifying in url.
Try it this way:
url: '@Url.Action("ReturnMethodTest", "Home")'
本文标签: jquerySyntaxError invalid regular expression flag ajaxJavaScriptStack Overflow
版权声明:本文标题:jquery - SyntaxError: invalid regular expression flag ajax, Javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1737405996a1471626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


url: '@Url.Action("ReturnMethodTest", "HomeController")'– Umesh Sehta Commented Jul 13, 2015 at 6:55