开发者

Passing value from multiline textbox using jQuery ajax

开发者 https://www.devze.com 2023-02-09 08:07 出处:网络
I would like to pass the content from a multiline textbox into an sql database using jQuery .ajax. function UpdateMemogramContent() {

I would like to pass the content from a multiline textbox into an sql database using jQuery .ajax.

function UpdateMemogramContent() {    
$.ajax({
        type: "POST",
        url: "MemogramWebServices.asmx/UpdateMemogramContent",
开发者_如何学运维        data: "{ 'mId': " + $("#LabelId").text() + ", 'content': " + $("#TextBoxContent").text() + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: Success,
        error: Error
    });
}

The problem I am facing is that the content from the multiline textbox is throwing an invalid json primitive exception. Taking a look at the POST:

{ 'mId': 314, 'content': Test

Test}

What can I do to pass the text from a multiline textbox into an sql database using .ajax?


Put a breakpoint before call posting data and look what value is you sending to server. Also

  1. change

$("#TextBoxContent").text()

to

$("#TextBoxContent").val()

  1. if p.1 did not help; try replace

data: "{ 'mId':

to

data: "{ mId: "


Why not use an actual JavaScript structure like so. This will however pass mId and content as standard post parameters rather than one piece of JSON.

$.ajax({
    type: "POST",
    url: "MemogramWebServices.asmx/UpdateMemogramContent",
    data: { 
        mId:     $("#LabelId").text(), 
        content: $("#TextBoxContent").text()
    },
    ...
});

Otherwise, you can also use JSON.stringify like so:

var json_data = JSON.stringify({ 
    mId:     $("#LabelId").text(), 
    content: $("#TextBoxContent").text()
});

$.ajax({
    type: "POST",
    url: "MemogramWebServices.asmx/UpdateMemogramContent",
    data: json_data,
    ...
});
0

精彩评论

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