Passing value from multiline textbox using jQuery ajax
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
- change
$("#TextBoxContent").text()
to
$("#TextBoxContent").val()
- 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,
...
});
精彩评论