问题描述:

I want to get data from code behind C# file in my .aspx page using jquery and ajax. But I am getting the Parse Error and function is not calling.

 $.ajax({

type: "GET",

url: "/Pages/Maps.aspx/getTitles",

contentType: "application/json; charset=utf-8",

data: { },

dataType: "json",

error: function (XMLHttpRequest, textStatus, errorThrown) {

alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);

},

success: function (msg) {

alert(msg)} });

and function is

[WebMethod]

public static string[] getTitles()

{

SPListItemCollection obj = RAK.DAL.DAL.GetSPListItemCollection("", "Map", null, 0, true, true);

SPQuery query = new SPQuery();

string[] titles = new string[100];

int count = 0;

foreach (SPListItem item in obj)

{

titles[count]= item["Title"].ToString();

count++;

}

return titles;

}

But I am getting the parseerror .

still getting the error

"Invalid JSON: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns:o="urn:schemas-microsoft-com:office:office" lang="en-us" dir="ltr">

<head><meta name="GENERATOR" content="Microsoft SharePoint" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Expires" content="0" /><meta http-equiv="X-UA-Compatible" content="IE=8"/><meta name="ROBOTS" content="NOHTMLINDEX" /><title>

Error

</title><link rel="stylesheet" type="text/css" href="/_layouts/15/1033/styles/corev15.css?rev=ox%2BqLd6WTqhn6d%2FMqf2BMw%3D%3D"/>

<link rel="stylesheet" type="text/css" href="/_layouts/15/1033/styles/error.css?rev=nc1850SZNy60qTAeQIRxsA%3D%3D"/>

<script type="text/javascript" src="/_layouts/15/init.js?rev=%2FWTxr8UesytJ8GB%2F5hOwZA%3D%3D"></script>

<script type="text/javascript" src="/ScriptResource.axd?d=LZxW7hnTQVulbq59cK9fzlaphpxU6f5G_dw5onCyJb_TxIsXp4cIkikqg_Do0Z8-pcG0ZTkniKbujTVnenPgA3EhA4aes3YaP29STXXQ7-iFBIAQllCJbBU-eJB6eTjGBH1eY-x9oBPNqZMic0vifOQNEiiO9xTmOFDF463fgRbWizMpQI3H_1H6Oa4HYQ5T0&amp;t=ca758f3"></script>

<script type="text/javascript" src="/_layouts/15/blank.js?rev=ZaOXZEobVwykPO9g8hq%2F8A%3D%3D"></script>

<script type="text/javascript" src="/ScriptResource.axd?d=71O5c0e-oC2hSftijXGS51A6RuoTvzsIJcuCvoZgaYCiprQFPaGXzIRVmF9Sr3_21k34YyFR2vfsVr9ZpN3D34dSvx5Uyg-wQa4F-u0OyPgjmmUbOqULBCR82iOkFImc9Mjlev1HewCPVj528MTf-bDALzDtM48JcLzLeds6Rlrfs8atXpFnazLO_oLjY6wq0&amp;t=ca758f3"></script>

<script type="text/javascript">RegisterSod("initstrings.js", "\u002f_layouts\u002f15\u002f1033\u002finitstrings.js?rev=4Yrxyggg5knao3D48Ii\u00252FWA\u00253D\u00253D");</script>

<script type="text/javascript">RegisterSod("strings.js", "\u002f_layouts\u002f15\u002f1033\u002fstrings.js?rev=u\u00252B0KcZWR52dtr8LTlqcZcw\u00253D\u00253D");RegisterSodDep("strings.js", "initstrings.js");</script>

<script type="text/javascript">RegisterSod("sp.init.js", "\u002f_layouts\u002f15\u002fsp.init.js?rev=3nSw25FIGbfepznMSgi74A\u00253D\u00253D");</script>

<script type="text/javascript">RegisterSod("sp.res.resx", "\u002f_layouts\u002f15\u002fScriptResx.ashx?culture=en\u00252Dus\u0026name=SP\u00252ERes\u0026rev=yNk\u00252FhRzgBn40LJVP\u00252BqfgdQ\u00253D\u00253D");</script>

<script type="text/javascript">RegisterSod("sp.ui.dialog.js", "\u002f_layouts\u002f15\u002fsp.ui.dialog.js?rev=0xf6wCIW4E1pN83I9nSIJQ\u00253D\u00253D");RegisterSodDep("sp.ui.dialog.js", "sp.init.js");RegisterSodDep("sp.ui.dialog.js", "sp.res.resx");</script>

<script type="text/javascript">RegisterSod("core.js", "\u002f_layouts\u002f15\u002fcore.js?rev=uA2xjCXmuYM5ARP8g3eTSA\u00253D\u00253D");RegisterSodDep("core.js", "strings.js");</script>

<script type="text/javascript">RegisterSod("foldhyperlink.js", "\u002f_layouts\u002f15\u002ffoldhyperlink.js?rev=JnjM4kpe0i1y9awK3p5UJw\u00253D\u00253D");</script>

<meta name="Robots" content="NOINDEX " />

<meta name="SharePointError" content="0" />

<link rel="shortcut icon" href="/_layouts/15/images/favicon.ico?rev=23" type="image/vnd.microsoft.icon" /></head>

<body id="ms-error-body" onload="if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();">

<form method="post" action="../../_layouts/15/error.aspx" id="aspnetForm" onsubmit="if (typeof(_spFormOnSubmitWrapper) != &#39;undefined&#39;) {return _spFormOnSubmitWrapper();} else {return true;}">

<div class="aspNetHidden">

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />

<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPBSpWU0tleTpiYjIxMTAyOC02ZTU1LTRmZjItYjExMi0zZGI2NzZhOGNkODlkwAVanAsLlnSOBjseddm8m+LJNm0tCUHtQbt8XAqMWSQ=" />

</div>

<script type="text/javascript">

//<![CDATA[

var theForm = document.forms['aspnetForm'];

if (!theForm) {

theForm = document.aspnetForm;

}

function __doPostBack(eventTarget, eventArgument) {

if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

theForm.__EVENTTARGET.value = eventTarget;

theForm.__EVENTARGUMENT.value = eventArgument;

theForm.submit();

}

}

//]]>

</script>

<script src="/WebResource.axd?d=46_jFpiwx-U4WjjexINQ2HKRkdrFpY_G6tP12wwFHIXkZVC1M8tpx2lvqX6yVVbJnJe2Ebiz_-xfS-bMSkJa2OnZLSTTK9e7DwFVaSjNv8k1&amp;t=635147041040000000" type="text/javascript"></script>

<script type="text/javascript">

//<![CDATA[

var g_presenceEnabled = true;

var g_wsaEnabled = false;

var g_wsaQoSEnabled = false;

var g_wsaQoSDataPoints = [];

var g_wsaLCID = 1033;

var g_wsaListTemplateId = 850;

var g_wsaSiteTemplateId = 'BLANKINTERNET#0';

var _fV4UI=true;var _spPageContextInfo = {webServerRelativeUrl: "\u002f", webAbsoluteUrl: "http:\u002f\u002ftest-vm2:17344", siteAbsoluteUrl: "http:\u002f\u002ftest-vm2:17344", serverRequestPath: "\u002fPages\u002fMaps.aspx\u002fgetTitles", layoutsUrl: "_layouts\u002f15", webTitle: "RAK - Customs", webTemplate: "53", tenantAppVersion: "0", webLogoUrl: "_layouts\u002f15\u002fimages\u002fsiteicon.png", webLanguage: 1033, currentLanguage: 1033, currentUICultureName: "en-US", currentCultureName: "en-US", clientServerTimeDelta: new Date("2014-11-17T09:57:37.2309622Z") - new Date(), siteClientTag: "0$$15.0.4569.1000", crossDomainPhotosEnabled:false, webUIVersion:15, webPermMasks:{High:2147483647,Low:4294967295},pageListId:"{5dd26c64-eeed-446f-8e79-d3470e6364db}",pageItemId:3, pagePersonalizationScope:1,userId:1, systemUserKey:"i:0\u0029.w|s-1-5-21-854245398-2146720035-839522115-54519", alertsEnabled:false, siteServerRelativeUrl: "\u002f", allowSilverlightPrompt:'True'};//]]>

</script>

<script src="/_layouts/15/blank.js?rev=ZaOXZEobVwykPO9g8hq%2F8A%3D%3D" type="text/javascript"></script>

<script type="text/javascript">

//<![CDATA[

if (typeof(DeferWebFormInitCallback) == 'function') DeferWebFormInitCallback();//]]>

</script>

<script type="text/javascript">

//<![CDATA[

Sys.WebForms.PageRequestManager._initialize('ctl00$ScriptManager', 'aspnetForm', [], [], [], 90, 'ctl00');

//]]>

</script>

<div id="ms-error-header" class="ms-pr">

<h1 class="ms-core-pageTitle">

<div id="ctl00_PlaceHolderPageTitleInTitleArea_ErrorPageTitlePanel">

Sorry, something went wrong

</div>

</h1>

<div>

</div>

</div>

<div id="ms-error">

<div id="ms-error-top">

</div>

<div id="ms-error-content">

<div id="ms-error-error-content">

<div id="DeltaPlaceHolderMain">

<div>

<span id="ctl00_PlaceHolderMain_LabelMessage">An unexpected error has occurred.</span>

</div>

<div id="ctl00_PlaceHolderMain_FoldPanel" class="ms-error-detailsFold">

<div id="ctl00_PlaceHolderMain_FoldLink_foldLinkWrapper">

<div id="ctl00_PlaceHolderMain_FoldLink_foldLinkPanel" class="ms-foldHyperLink-panel" onclick="EnsureScriptParams(&#39;foldhyperlink.js&#39;, &#39;ToggleFoldText&#39;, &#39;d0452adf743844cbb1f19a53f1048f2d&#39;, &#39;ctl00_PlaceHolderMain_FoldLink__moreDetailsLink&#39;, &#39;Technical Details&#39;, &#39;Technical Details&#39;);;return false;">

<a id="ctl00_PlaceHolderMain_FoldLink__moreDetailsLink" class="ms-commandLink ms-floatLeft" href="#" onclick="PreventDefaultNavigation();">Technical Details</a>

<div class="ms-clear"></div>

</div>

</div><div class="ms-hide" id="d0452adf743844cbb1f19a53f1048f2d">

<div>

<p>

<span class="ms-descriptiontext">

</span>

</p>

<p>

<span class="ms-descriptiontext">

<div id="ctl00_PlaceHolderMain_FoldLink_WSSEndUser_troubleshootingPanel">

<span id="ctl00_PlaceHolderMain_FoldLink_helptopic_WSSEndUser_troubleshooting"><a title="Troubleshoot issues with Microsoft SharePoint Foundation. - Opens in new window" href="javascript:HelpWindowKey('WSSEndUser_troubleshooting')">Troubleshoot issues with Microsoft SharePoint Foundation.</a></span>

</div>

</span>

</p>

<p>

<span id="ctl00_PlaceHolderMain_FoldLink_RequestGuidText" class="ms-metadata">Correlation ID: c7f5cc9c-abed-10fd-5a6e-22308fdc420d</span>

</p>

<p>

<span id="ctl00_PlaceHolderMain_FoldLink_DateTimeText" class="ms-metadata">Date and Time: 11/17/2014 2:57:37 PM</span>

</p>

</div>

</div>

</div>

<div class="ms-error-techMsg">

<hr />

</div>

<script type="text/javascript">// <![CDATA[

var gearPage = document.getElementById('ms-loading-box');

if(null != gearPage)

{

gearPage.parentNode.removeChild(gearPage);

document.title = "Error";

}

function _spBodyOnLoad()

{

var intialFocus = (document.getElementById("ctl00_PlaceHolderMain_FoldLink__moreDetailsLink"));

try

{

intialFocus.focus();

}

catch(ex)

{

}

}

// ]]>

</script>

</div>

</div>

<div id="ms-error-gobackcont" class="ms-calloutLink">

<a href="/" class='ms-calloutLink' id="ctl00_PlaceHolderGoBackLink_idSimpleGoBackToHome">Go back to site</a>

</div>

</div>

</div>

</form>

</body>

</html>"

function is going into success but showing the above msg. anyone please help me

网友答案:

Your Are missing [WebMethod] Before Method(You need to Add Namespace System.Web.Services.WebMethod)

using System.Web.Services.WebMethod

[WebMethod]
public static string[] getTitles()
            {
                SPListItemCollection obj = RAK.DAL.DAL.GetSPListItemCollection("", "Map", null, 0, true, true);
                SPQuery query = new SPQuery();

                string[] titles = new string[100];
                int count = 0;
                foreach (SPListItem item in obj)
                {
                    titles[count]= item["Title"].ToString();
                    count++;
                }
                return titles;
            }

Reference

相关阅读:
Top