0

I have written a function in javascript

function abc()
{
   var Url = "MyService.svc/MyMethod";
   var Param = '{"Keyword":"' + Keyword + '","Type":"' + type + '"}';
   $.ajax({
      type: "POST",
      url: Url,
      data: Param,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (msg) {
         var pdResults = msg.MyServiceResult;
      },
      error: function (xhr, ajaxOptions, thrownError) {
      }
   });
}

//---------------------------------------------------

on the server side I have written method

public string MyMethod(string Keyword, string Type)
{
  return "1";
}

//--------------------------------------------------- Problem :

Problem is Javascript method never goes into success method. in error method it shows

parse error: Unexpected token <

I don't know what part is causing this problem. I couldn't find a solution, can somebody fix it.

T J
  • 40,740
  • 11
  • 73
  • 131
  • 1
    The code you shared doesn't reproduce this. it's probable coming from somewhere else... – T J Sep 11 '14 at 06:59

2 Answers2

0

I think you should return "{ result: 1}" or "{ \"result\": 1 }" in your MyMethod. Because jQuery trys to parse the response as a valid JSON string, but what you response is 1, which is not valid.

See http://json.org for more infomation.

Kevin Yue
  • 133
  • 1
  • 6
  • I have tried that it didn't work. When I write dataType as "jsonp" it calls server side method but if I write it as "json", it doesn't call server side method. – Mayur Mohite Sep 11 '14 at 07:21
  • Change the `type: "POST"` to `type: "GET"` and try again. – Kevin Yue Sep 11 '14 at 07:30
  • POST to GET...It worked...! :) Thanx man! But its weird POST isn't working – Mayur Mohite Sep 11 '14 at 08:03
  • I think you should change your server side code, but I'm not familiar with .NET. Here is a link, maybe it could help you. http://stackoverflow.com/questions/41155/wcf-service-returning-method-not-allowed – Kevin Yue Sep 11 '14 at 08:20
0

I think you should monitor in developer tools or tools like fiddler that what parameters are actually being posted to the server.

Mihir
  • 309
  • 2
  • 9