Uncaught SyntaxError: Invalid regular expression flags(看頁面源代碼)
一、總結
js或者jquery方面的錯誤看頁面源代碼,一下子錯誤就很清晰了
二、Uncaught SyntaxError: Invalid regular expression flags
頁面用jquery中的ajax的時候出現這個錯誤
1 $(document).ready(function(){ 2 $("#b01").click(function(){ 3 htmlobj=$.ajax({url:"/jquery/test1.txt",async:false}); 4 $("#myDiv").html(htmlobj.responseText); 5 }); 6 });
網上找的解答是:
@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.
看了下頁面動態html轉換成的靜態html(通俗說就是頁面源代碼):很容易就發現錯誤了
三、其它錯誤參照
問題:
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); } });
解答:
@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.
url: '@Url.Action("ReturnMethodTest", "HomeController")'
– Mohit Kumar Jul 13 '15 at 6:55