最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass a variable to webservice in $.ajax - Stack Overflow

programmeradmin5浏览0评论

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':''}",
    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}",

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.'}",
    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}",
Share Improve this question edited May 30, 2009 at 6:17 cletus 626k169 gold badges919 silver badges945 bronze badges asked May 30, 2009 at 5:56 MiniMini
Add a ment  | 

4 Answers 4

Reset to default 3

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

$(function() {
  var dynamic_url = "http://www.example.";
  $.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.

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 mon 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.

The following should work:

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

    $.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.

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

data:"{\"url\":\"" + x + "\"}",
发布评论

评论列表(0)

  1. 暂无评论