开发者

AJAX function to webmethod doesnt work

I have a 2 ajax functions on my page. One that works when a link inside a grid is clicked and one when a button is clicked. The one from the grid works perfectly. However, the function from the click of a button produces an error every time.

Can someone please tell me where i am going wrong here. I would appreciate any help. Here is the one that fails:

    function createSource() {
        $.ajax({
            type: "POST",
            url: "Test.aspx/createSource",
            data: '{"schoolID":"' + $('#ddSchools').val() + '","vendor":"' + $('#txtVendor').val() + '","tSource":"' +  $('#txtTSource').val()+ '"}',
            contentType: "application/json",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (xhRequest, ErrorText, thrownError) {
                alert(thrownError);
            }
        });
    }

The Webmethod will have more code to it but i cant seem to hit it anyway. Here is the webmethod:

[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string createSource(int schoolId, string vendor, string tSource)
{
    try
    {
        string status = "This is a test string!";

        return status;
    }
    catch
    {
        throw new Exception("Could not create source code!");
    }
}

Also how can i go about getting the exact error that is causing this function to fail?

Thank you in advance for any help!!


ok so i开发者_开发知识库 figured where the problem is but i still do not have a solution. There actually wasn't anything wrong with the code. I plugged the code in at document.ready and it fired correctly. However, it will not fire correctly whenever the button is clicked. The button is nothing special either. It is a simple input button.

<input id="cmdSubmit_Create" value="Submit" type="submit" />

any ideas?

For reference i will put the code that works on the document.ready:

$(document).ready(function () {
    $.ajax({
            type: "POST",
            url: "Test.aspx/createSource",
            data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}',
            contentType: "application/json",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("status: " + textStatus + " errorThrown: " + errorThrown);
            },
            complete: function (jqXHR, textStatus) {
                alert("status: " + textStatus);
            }
        });
});

[WebMethod]
public static string helloWorld(string schoolId, string vendor,string tsource)
{
    try
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("Hello World! " + schoolId + '-' + vendor + '-' + tsource);

        return sb.ToString();
    }
    catch
    {
        throw new Exception("Could not create source code!");
    }
}

If i try to call the same webmethod on the click of cmdSubmit_Create it doesnt work:

       $('#cmdSubmit_Create').click(function () {
        $.ajax({
            type: "POST",
            url: "Test.aspx/helloWorld",
            data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}',
            contentType: "application/json",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (xhr, status, error) {
                alert('error' + error)
            }
        });


use following code

$('#cmdSubmit_Create').click(function () {
    $.ajax({
        type: "POST",
        url: "Test.aspx/helloWorld",
        data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}',
        contentType: "application/json",
        dataType: "json",
        success: function (msg) {
            alert(msg.d);
        },
        error: function (xhr, status, error) {
            alert('error' + error)
        }
    });
    return false;
});

http://cmsnsoftware.blogspot.com/2011/01/how-to-call-csharp-function-in-ajax.html


you service expects a GET UseHttpGet = true where as you are doing POST try instead

type: "GET",
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜