Home > front end >  Ajax how to pass a string to a webservice, prompt the web service invocation is invalid, parameter &
Ajax how to pass a string to a webservice, prompt the web service invocation is invalid, parameter &

Time:09-19

 $(" # btn1 "). Click (function () {
Var jsonObj={" account ":" value1 ", "contant" : "value2" "Detail" : [{" product ":" value3 ", "model" : "value4", "QTY" : 1}]}
;
Var jsonStr=JSON. Stringify (jsonObj);
//alert (jsonStr);
$. Ajax ({
Type: "POST",
ContentType: "application/json; Charset=utf-8,
"Async: false,
DataType: "json",
Url: "http://localhost:8078/data.asmx/HelloWorld",
Data: jsonStr,
Success: the function (data) {
Alert (data. D);
},
Error: function (data) {
Alert ($. ParseJSON (data. The responseText). The Message). },
});
});


[the WebMethod]
 
Public string HelloWorld (string jsonStr)
{

Return "Hello" + jsonStr;
}



CodePudding user response:

JsonObj={" account ", "value1", "contant" : "Detail" : [{" product ":" value3 ", "model" : "value4", "QTY" : 1}]}
The write wrong json format itself has a problem

CodePudding user response:

reference 1st floor usecf response:
jsonObj={" account ":" value1 ", "contant" : "Detail" : [{" product ":" value3 ", "model" : "value4", "QTY" : 1}]}
The write wrong json format itself has a problem of


Thanks for checking, I change after, or such an error,

CodePudding user response:

Take a look at your jsonObj to mend

CodePudding user response:

reference usecf reply: 3/f
to look at you to mend jsonObj issued



$(" # btn1 "). Click (function () {
Var jsonObj={" OPNAME ":" testname ", "APPLYNO" : "AbbrOP01", "OPACID:" 3424, "OPAMT:" 1000, "OPCURR" : "CNY", "OPCREA" : "LJY", "OPOWNR" : "RFS," "OPNEED" : "XUQIU", "OPCPTI" : "MINGXE", "OPCOMP" : "LUOSHI", "Shipto" : "10001", "Detail" : [{" OUPROD ":" WWREWS ", "OUMODE" : "WERWED", "OUQTY" : 1, "OUEXPP" : 1, "OUMINQ" : 1}]}.
//var jsonStr=JSON. Stringify (jsonObj);
//alert (jsonStr);
$. Ajax ({
Type: "POST",
ContentType: "application/json; Charset=utf-8,
"Async: false,
DataType: "json",
Url: "http://localhost:8078/data.asmx/HelloWorld",
//data: {" jsonStr ":" {\ "account " : \ "value1 " and \ "contant " : \ "value2 "} "},
Data:????? ,
Success: the function (data) {
Alert (data. D);
},
Error: function (data) {
Alert ($. ParseJSON (data. The responseText). The Message). },
});
});

CodePudding user response:

reference 4 floor qq_16630463 response:
Quote: reference usecf reply: 3/f

Sending all of you to turn jsonObj to see



 $(" # btn1 "). Click (function () {
Var jsonObj={" OPNAME ":" testname ", "APPLYNO" : "AbbrOP01", "OPACID:" 3424, "OPAMT:" 1000, "OPCURR" : "CNY", "OPCREA" : "LJY", "OPOWNR" : "RFS," "OPNEED" : "XUQIU", "OPCPTI" : "MINGXE", "OPCOMP" : "LUOSHI", "Shipto" : "10001", "Detail" : [{" OUPROD ":" WWREWS ", "OUMODE" : "WERWED", "OUQTY" : 1, "OUEXPP" : 1, "OUMINQ" : 1}]}.
//var jsonStr=JSON. Stringify (jsonObj);
//alert (jsonStr);
$. Ajax ({
Type: "POST",
ContentType: "application/json; Charset=utf-8,
"Async: false,
DataType: "json",
Url: "http://localhost:8078/data.asmx/HelloWorld",
//data: {" jsonStr ":" {\ "account " : \ "value1 " and \ "contant " : \ "value2 "} "},
Data: JSON. Stringify ({" jsonStr ": JSON. Stringify (jsonObj)}),
Success: the function (data) {
Alert (data. D);
},
Error: function (data) {
Alert ($. ParseJSON (data. The responseText). The Message). },
});
});

CodePudding user response:

Do you want to
JsonObj variable string quotes

JsonObj='{" OPNAME ":" testname ", "APPLYNO" : "AbbrOP01", "OPACID:" 3424, "OPAMT:" 1000, "OPCURR" : "CNY", "OPCREA" : "LJY", "OPOWNR" : "RFS," "OPNEED" : "XUQIU", "OPCPTI" : "MINGXE", "OPCOMP" : "LUOSHI", "Shipto" : "10001", "Detail" : [{" OUPROD ":" WWREWS ", "OUMODE" : "WERWED", "OUQTY" : 1, "OUEXPP" : 1, "OUMINQ" : 1}]}'


Then you have to pass a json object with the following
Var jsonStr=JSON. Stringify (jsonObj);

CodePudding user response:

The content of the second layer nested remove [], you try again

$. Ajax ({
Type: "POST",
Url: "http://localhost:8078/data.asmx/HelloWorld",
ContentType: "application/json",//must be
DataType: "json",//the return value type, must not
Data: JSON. Stringify ({
"OPNAME" : "testname",
"APPLYNO" : "AbbrOP01,"
"OPACID" : "3424",
"Detail" : {
"OUPROD" : "WWREWS,"
"OUMODE" : "WERWED,"
"OUQTY" : "1", "OUEXPP" : "1", "OUMINQ" : "1"
}
}),
Success: the function (res) {
console.log(res);
}
});
  •  Tags:  
  • Ajax
  • Related