views:

451

answers:

4

I want to send a parameter to the webservice. The parameter needs to be a variable and not a fixed string.When I write the following code the webservice is called fine and is executed perfectly.

$(document).ready(function() {
  $.ajax({
    type: "POST",
    url: "JsonTestService.asmx/Test",
    data:"{'url':'http://www.cramster.com'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      alert(msg.d);
    }  
  });
});

But when I change the line to the following where x is a variable,it doesn't work . Can you tell me how to pass a variable to the webservice in the following code.

data:"{'url':x}",
+1  A: 

You were really close. Don't enclose your data element value in quotes ie:

$(function() {
  var dynamic_url = "http://www.example.com";
  $.ajax({
    type: "POST",
    url: "JsonTestService.asmx/Test",
    data: {
      url: dynamic_url
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      alert(msg.d);
    }
  });
});`

By enclosing the whole lot in quotes the expression simply wasn't evaluated. I would also advise using the both syntax for passing in objects. I think its clearer.

cletus
Since he's using an ASP.NET AJAX "ScriptService", quoting the entire data parameter is required. ASP.NET expects a JSON string as parameter, not POST variables.
Dave Ward
@Dave: Does jQuery encode the data object as form parameters even when you specify the dataType is 'json' in such a way that ASP.NET really can't recognize it?
cletus
It does. Semi-related: jQuery also won't honor the contentType parameter unless there's a data parameter. So, read-only calls to ASMX services must be made with a "blank" data parameter of "{}".
Dave Ward
+1  A: 

The following should work:

$(document).ready(function() {
    var x = 'http://www.cramster.com';

    $.ajax({
        type: "POST",
        url: "JsonTestService.asmx/Test",
        data: { 'url': x },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        }
    });
})

You need to lose the quotes around your data parameter.

Ronald Wildenberg
I have tried both the ways. But none of them is still working
$(document).ready(function() { var x = 'http://www.cram.com'; $.ajax({ type: "POST", url: "JsonTestService.asmx/Test", data:"{'url':'"+x+"'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); } });});works perfectThanks for all the help
Try this: var x = "{'url':'http://www.cramster.com'}"...data: x....That should send exactly the same to the server as your static version.
Frans
+2  A: 

The reason why you must quote the entire JSON string:

In requests made to them, ASP.NET AJAX script services and page methods understand and expect parameters serialized as JSON strings. These parameters are parsed out of the POST data and used as arguments to the method you’ve called.

However, if you directly provide a JSON object as the data parameter for an $.ajax call, jQuery will serialize the object as a series of k,v pairs to be POSTed instead of passing the raw JSON to your web service.

More detail is available in this post on common pitfalls when using jQuery with ASP.NET AJAX web services and page methods, if you're interested.

You may also be interested in using a stringify method to make constructing the JSON string much cleaner.

Dave Ward
+1  A: 

It is correct to have the complete json as a string per the jquery docs, the obvious solution to getting value of x into it is to do:

data:"{\"url\":\"" + x + "\"}",
PQW
Don't do that, you'll break the JSON.
alamar
I fail to see how it's breakign the JSON, but sure, if you can use a stringify method that would be better, but that is yet another javascript to include then on the page.
PQW