0

Why suddenly my working code start giving me error Unexpected token < in JSON at position when doing jQuery AJAXin asp.net web forms

$.ajax({
    type: "POST",
    url: "About/UpdateProduct",
    contentType: "application/json; charset=utf-8",
    data: '{"productData":"' + newValues + '"}',
    dataType: "json",
    success: function (result, status, xhr) {
        alert(result);
    },
    error: function (xhr, status, error) {
        console.log(error)
    }
});

[WebMethod]
public static string UpdateProduct(string productData)
{
    return "abc";
}

The WebMethod is not getting called now and I get the error.

The response on the network tab:

GENERAL:
Request URL: http://localhost:61131/About/UpdateProduct
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:61131
Referrer Policy: no-referrer-when-downgrade

RESPONSE HEADERS:
Cache-Control: private
Content-Length: 14796
Content-Type: text/html; charset=utf-8
Date: Sat, 08 Dec 2018 14:04:00 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcWW9nZXNoXERvY3VtZW50c1xWaXN1YWwgU3R1ZGlvIDIwMTdcUHJvamVjdHNcV2ViRm9ybXNcV2ViRm9ybXNcQWJvdXRcVXBkYXRlUHJvZHVjdA==?=

REQUEST HEADERS:
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 29
Content-Type: application/json; charset=UTF-8
Cookie: _ga=GA1.1.1479180893.1539165747
Host: localhost:61131
Origin: http://localhost:61131
Referer: http://localhost:61131/About
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36
X-Requested-With: XMLHttpRequest

REQUEST PAYLOADS:
{productData: "Pants,5,10,"}
productData: "Pants,5,10,"

The response which i get from the Network tab is a full html:

<!DOCTYPE html>

<html lang="en">
<head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>
    About - My ASP.NET Application
</title><script src="/Scripts/modernizr-2.6.2.js"></script>
<link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/Site.css" rel="stylesheet"/>
<link href="../favicon.ico" rel="shortcut icon" type="image/x-icon" /></head>
<body>
    <form method="post" action="./UpdateProduct" id="ctl01">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="ptdu7lNxa+5SO8z3wDYkLbKPdfrJ2Dw5hEQHjWfyhwKPqFz4Xj+gJqzPDiw8ZyM1qfPDKjSsETY2TIgaw2KJQEhibFMpq6HpusgQ8+5K3k9pAn5HOPxV3Dwz5IIktj5BuD0z/v+lKYMVzTPzNnpB8CguhjtVvE3vm1yGBuyGqczDsBAZXrrBxFYYc9WLcXtJcby8BB3biF+//vorksAU0u4CjEFutY5YHxjZebMBe0kcohzCxateJgFdGtux9zY89QMoPRh6CYLyKy2mx6QkYTjVy2Jz9oHB4NHCIc9ZIgtCYszhc00XrCEC0CRSS0mcG10Y6wGRGRJ38nwGVmyU7NhP9oAi4/zgtItcRffuaO+OpgzcNRXq7oTyZ2Cklp3mDt1uFGP4uusmWH3B6J/W3jxXSzdfYgVuVqbUImUM0hUhhiim/fIoFUiXiA2grkV9mBoINc2QkyLqP5FwFI65BkqIMRL+frEK7jRxv9tndVP6NDuTdJEGYaKlZZ+PqOcshElE34qFUUDC5/uZjLtabEYk760R6PRaAgrVMYf/ez/2t9v55KRdhSctF9y3YFDrBaPMJWlnITya0Fx/zKoD+NvnssRyGqdaSyey3yJJPO8mCUisgaaxP1hkXQvkjX0xA2Hbp0IbwzDqY3MVg13Yg9gfYhpPK9mfztGcCuLunjoBarAD9kdFzKbHH82rPhVgJtArrzGTBab5/LXFIL6Rrit36HxtN0kNRz9eSNKgq6vT/ZK6olg8xBVUNMhPs5nXN39ZG43othx7gWZmloO8anUZD7PcIeUNVZJzHRzY+S7fiehLcA+pT9dc0nJxwyGCpaIoZWxuHGYjVKn6YK8qZeYaEUebSWhj/+xdDj6ZEdr1MvOLKUOlHD/fT+4LKqHgvDU0fgM0KCa65B+it4/OmM5Yjg8R0x05R0+oGmu0MoetUyLKSPtoyhHEkhBfAzD3xTavnOz11dgYa3II2Ct/Xn6bHBgk+i53ST2ByQIx9MEB+O0zFphNFpzVDlatMwniqaeDLEEpv1MDNR/tkoj/6g==" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl01'];
if (!theForm) {
    theForm = document.ctl01;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

In my jQuery code i have already mentioned:

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

So why I am getting HTML? Same code work working properly some time back.

Please help?

yogihosting
  • 5,494
  • 8
  • 47
  • 80
  • In your browser's debugging tools, check the network tab. What is the exact content being returned from the server by the AJAX call? – David Dec 08 '18 at 13:47
  • The call for `About/UpdateProduct` doesn't return JSON but XML/HTML – Andreas Dec 08 '18 at 13:48
  • @david it's 200 message. – yogihosting Dec 08 '18 at 13:48
  • @yogihosting: And what is the *content*? – David Dec 08 '18 at 13:50
  • @Andreas how to return json from C# method. Earlier the same code was working in previous version of .net but now it stopped unexpectedly. – yogihosting Dec 08 '18 at 13:50
  • @David Content-Type: text/html; charset=utf-8 – yogihosting Dec 08 '18 at 13:53
  • @Andreas I tried return JsonConvert.SerializeObject("abc"); but no use. – yogihosting Dec 08 '18 at 13:57
  • @yogihosting: You're still looking at *only* the headers. You have to look at *all* of the information being returned. What is the body of the response? The actual content? The data that it is returning? *What is being returned from the server?* – David Dec 08 '18 at 13:58
  • @David I updated the answer with full Request and Response info from network tab. – yogihosting Dec 08 '18 at 14:08
  • 1
    _"What is the **body** of the response?"_ – Andreas Dec 08 '18 at 14:11
  • @yogihosting: `Content-Length: 14796` - That seems like a lot more data than simply `return "abc";` would return. If it's not too much trouble, in order to help debug your problem, could you actually bother to take a moment and ***LOOK AT THE BODY OF THE RESPONSE***. – David Dec 08 '18 at 14:12
  • @Andreas I get response (in network tab) as full html of the page. I have posted this HTML on the question now. – yogihosting Dec 08 '18 at 14:14
  • @David I get HTML response, why? I have contentType: "application/json; charset=utf-8", and dataType: "json",??? – yogihosting Dec 08 '18 at 14:15
  • Hello coders, the project is made from VS2017 asp.net web forms template. It has url rewriting from the beginning. So can URL rewriting is causing this problem? – yogihosting Dec 08 '18 at 14:20
  • @David I created a new web form project with empty template in VS 2017. I ran the same code and it worked properly. So i think its the problem with the template, when using the web forms template in 2017 which has inbuilt URL rewriting and other stuffs from the beginning then the same code stops working. I think the reason is the lots of javascript code that is injected into the web forms by default which may cause the problem. – yogihosting Dec 08 '18 at 14:41
  • @Andreas I got the solution, it is cause by 'FriendlyURL' thing. The solution is https://stackoverflow.com/questions/32663858/jquery-ajax-calls-not-working-with-asp-net-web-forms-when-friendlyurls-are-added. Thank you for your help. – yogihosting Dec 08 '18 at 15:04

0 Answers0