2

I have a Web Service that recive an JSON Object Prueba

public class Prueba
{
  public string valor1 { get; set; }
  public string valor2 { get; set; }
}

public JsonResult Pruebas(Prueba item)
{
   string metodo = Request.HttpMethod;
   return Json("error", JsonRequestBehavior.AllowGet);
}

And I want to call to the web service with JQuery:

$.ajax({
            type: 'Post',
            dataType: 'json',
            url: 'http://localhost:24780/Api/Pruebas',
            data:'{"valor1":"a","valor2":"b"}',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                console.debug(data);
            },
            error: function (data) {
                console.debug(data);
            }
        });

The problem is that Request.method takes the value OPTION instead of POST. Also the object value is null.

I have tested the web service with SOAP UI without problems but I can't find why it don't work with JQuery.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • You have a `POST` AJAX call, does your web-service method support `POST` or `GET`? – Vimal Stan Mar 26 '13 at 12:01
  • Also, please get rid of the MVC tag, I don't think it belongs on this question. – Vimal Stan Mar 26 '13 at 12:02
  • @Vimal The web-service support POST. I added the TAG now because I found that without the entry contentType: 'application/json; charset=utf-8' in ajax the Request.method takes the right value. But it still don't works because it doesn't recognize the JSON object. – JakeChambers Mar 26 '13 at 12:12
  • You might find the solution here: [Cross-domain POST AJAX requests](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – Vimal Stan Mar 26 '13 at 12:14
  • Thanks for your answer. Actually it doesn't work still I'd published the web service in a server. – JakeChambers Apr 04 '13 at 08:12

2 Answers2

0

You trying to send a string instead of an object. This is why on server error you retrieve anything but not an object. Try to replace this:

data:'{"valor1":"a","valor2":"b"}',

to this:

data:{"valor1":"a","valor2":"b"},
zelibobla
  • 1,498
  • 1
  • 16
  • 23
0

you are making your object as string by using single quotes around it. That is the reason you are not getting value of object

SP Singh
  • 416
  • 4
  • 18