2

I am working on a Google Chrome Extension. I have string array and I need post my array to web service with ajax and return again. Here is my code:

 var data = {};
data.param1 = words; // my string array

$.ajax({
    data: JSON.stringify( data ),
    dataType: "json",
    url: 'http://localhost:49242/Service.asmx',
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function (result) {
        alert("basarili");
    },
    failure: function(errMsg) {
    alert(errMsg);
}
});

Service:

public string example(string[] param1) {

        string result = param1[0];
        return result;
    }

I am not getting an error but this code is not working. How can I solve?

2
  • do you have debug your code step by step ? do you have see what you send, what you get ? Commented Apr 12, 2016 at 10:41
  • you mean param1[""param1"]? Commented Apr 12, 2016 at 10:41

3 Answers 3

1
var data = {};
data.param1 = words; // my string array

$.ajax({
    data: {param1: data.param1},
    url: 'http://localhost:49242/Service.asmx',
    type: "POST",
    success: function (result) {
        alert("basarili");
    },
    failure: function(errMsg) {
        alert(errMsg);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try using Json Encode while returning string in the Service file.

2 Comments

can you write an example?
For example, in your Service file you can convert string to JSON format and then return it. public string example(string[] param1) { string result = param1[0]; var serializer = new JavaScriptSerializer(); return serializer.Serialize(new { Name = result }); } Hope this helps you.
0

I have given similar answer in

Link

As per my observation

var data = {};
data.param1 = ["String1","String2"]; // my string array

$.ajax({
    data: JSON.stringify( data ),
    dataType: "json",
    url: 'http://localhost:49242/Service.asmx/example',//Missing Method Name 
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function (result) {
        alert(result.d);
    },
    failure: function(errMsg) {
    alert(errMsg);
}
});

and .aspx

  <form id="form1" runat="server">
    //rest is omitted 
    <asp:scriptmanager ID="Scriptmanager1" runat="server">
        <Services>
            <asp:ServiceReference runat="server" Path="~/WebService1.asmx"/>

        </Services>

    </asp:scriptmanager>
    </form>

and

[System.Web.Script.Services.ScriptService]//This is must
public string example(string[] param1) {

        string result = param1[0];
        return result;
    }

5 Comments

I don't have a web form (.aspx) . Is it a problem?
I tested in jQuery .It will work with out scriptmanager.The problem is in your method .check the ajax call .
Did ur web service works perfectly? What does ur result show? Paste ur word object?
Service is working perfectly. When I writed a string manually in url, return a value correctly. But I can't send a variable to service. I think the problem is here: $.ajax({ data: JSON.stringify(data), dataType: 'json', url: "Service.asmx/kok_bul", type: "POST", contentType: "application/json; charset=utf-8", success: function (result) { alert(result); }, failure: function (errMsg) { alert(errMsg); } });
alert(result) will not show anytingin asp.net . try alert(result.d)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.