Send just a string from $.ajax to c#

AngelicCore

This is supposedly very easy but for some reason it has taken me about 2 hours and countless searches and nothing is working

I am trying to call a WebMethod from ajax, and it works quite well. As soon as I try to change the c# function to accept parameters and send one from ajax everything fails

Code: c#:

[WebMethod]
public static string GetBGsForSelectedCrop(string cropName)
{
    return "asdasd";
}

jquery:

$().ready(function () {

        $("#Result").click(function () {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetBGsForSelectedCrop",
                data: "Wheat",
                success: function (msg) {
                    $("#Result").text(msg.d);
                    alert(msg.d);
                    console.log(msg)
                }
            });
        });
    });

I have tried datatype: "json", contentType: "application/json; charset=utf-8", and tried without both and datatype: "string" and datatype: "text", GET, data: "{'ABCD'}, data:{"cropName: Wheat"}, and data: json.Stringify("Wheat").

I get undefined for msg.d and sometimes HTTP error 500 if I take it too far.

What am I missing? It is just a simple task and should've been done in seconds..

Wilfredo P

As the guys in the comments says, you need to change your code for:

$("#Result").click(function () {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetBGsForSelectedCrop",
                data: JSON.stringify({ cropName: "Wheat" }),
                dataType:'text', 
                contentType: "application/json; charset=utf-8",
                success: function (msg) {
                    $("#Result").text(msg.d);
                    alert(msg.d);
                    console.log(msg)
                }
            });
        });

Your error is the data is no good encoded, and you are missing the datatype.

What is the stringfy It Convert any value to JSON.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Send JSON data as a string to AJAX from Java Servlet for Datatables

From Dev

Send dynamic string in AJAX data

From Dev

grab just the cents from string

From Dev

grab just the cents from string

From Dev

Send List<String> from iOS to C# WebMethod

From Dev

Read single string from file in C script and send it as part of an url

From Dev

Send a string from client to server, then save the file [using C]

From Dev

Send multiple arrays and a string using json from ajax to php and receiving them on php

From Dev

Send string to database with Ajax not working as intended

From Dev

Send string to php gd through ajax request

From Dev

javascript - send ajax request without string concatenation

From Dev

Cant Send Value To PHP String With AJAX

From Dev

How to send String data with formdata in ajax

From Dev

Extract just file path from string

From Dev

Extracting just a string element from a pandas dataframe

From Dev

Remove just the alphabet characters from a string

From Dev

How to get just a string from room database

From Dev

Remove just the alphabet characters from a string

From Dev

PHP extract just a specific number from a string

From Dev

Send string from service to activity

From Dev

Send String from HostApp to SmartExtension

From Dev

C# irc client streamwriter just send one word

From Dev

Getting just the number of a String C#

From Dev

Send Headers in Cross domain request From Ajax

From Dev

send array from ajax to Flask not working

From Dev

Send multidimentional array from JQuery AJAX to PHP

From Dev

How to send IEnumerable list from Ajax to Controller

From Dev

Send parameters with ajax call from jquery datatables

From Dev

Unable to send data to php from jquery ajax?

Related Related

  1. 1

    Send JSON data as a string to AJAX from Java Servlet for Datatables

  2. 2

    Send dynamic string in AJAX data

  3. 3

    grab just the cents from string

  4. 4

    grab just the cents from string

  5. 5

    Send List<String> from iOS to C# WebMethod

  6. 6

    Read single string from file in C script and send it as part of an url

  7. 7

    Send a string from client to server, then save the file [using C]

  8. 8

    Send multiple arrays and a string using json from ajax to php and receiving them on php

  9. 9

    Send string to database with Ajax not working as intended

  10. 10

    Send string to php gd through ajax request

  11. 11

    javascript - send ajax request without string concatenation

  12. 12

    Cant Send Value To PHP String With AJAX

  13. 13

    How to send String data with formdata in ajax

  14. 14

    Extract just file path from string

  15. 15

    Extracting just a string element from a pandas dataframe

  16. 16

    Remove just the alphabet characters from a string

  17. 17

    How to get just a string from room database

  18. 18

    Remove just the alphabet characters from a string

  19. 19

    PHP extract just a specific number from a string

  20. 20

    Send string from service to activity

  21. 21

    Send String from HostApp to SmartExtension

  22. 22

    C# irc client streamwriter just send one word

  23. 23

    Getting just the number of a String C#

  24. 24

    Send Headers in Cross domain request From Ajax

  25. 25

    send array from ajax to Flask not working

  26. 26

    Send multidimentional array from JQuery AJAX to PHP

  27. 27

    How to send IEnumerable list from Ajax to Controller

  28. 28

    Send parameters with ajax call from jquery datatables

  29. 29

    Unable to send data to php from jquery ajax?

HotTag

Archive