简体   繁体   中英

Post JSON data along with id in Jquery AJAX

I'm trying to post JSON data along with 2 ids through a Jquery AJAX post. But I am not able to do it.

Following is my code:

try {
    var surveyID= localStorage.getItem("surveyId");
    var userDetails = jQuery.parseJSON(localStorage.getItem("userdetails"));
    var providerKey = userDetails["ProviderUserKey"];
    var dataValue = { "str": StringJson};
    var url = APP_URL+"EditSurvey?";
    var param = "SurveyId="+surveyID+"&JSONData="+JSON.stringify(dataValue)+"&UserId="+providerKey;

    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: url,
           data: param,
           async:true,
        success: function (data) {

            alert('sucess');


            //}

        },  
        error: function (err) {
            alert("Err : " + err.error);
        },

    });
} catch (error) {
     alert(error);
}

I get the following error when I debug this in Safari:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

and in simulator I get the following error:

在此处输入图片说明

Where am I getting wrong? How do I solve this? I have to 3 parameters for post

  • surveyID
  • JSON data
  • userID

Edited:

The webservice is now changed and all 3 params- ie 2 ids and one whole json data is passed to the webservice. Still jquery ajax post is not working. See my code below:

 var surveyID= localStorage.getItem("surveyId");
    var userDetails = jQuery.parseJSON(localStorage.getItem("userdetails"));
    var providerKey = userDetails["ProviderUserKey"];
    var dataValue = {"surveyID":surveyID, "userID":providerKey, "str": StringJson};
    alert(dataValue);
    var url = APP_URL+"EditSurvey";
    var param = dataValue;
    $.ajax({
           type: 'POST',
           contentType: "application/json",
           url: url,
           data: dataValue,
           success: function (data) {

           alert('sucess');


           //}

           },
           error: function (err) {
           alert("Err : " + err.text);
           },

           });

edited to include stringJson:

var StringJson = JSON.stringify(MainJSON);
 alert(StringJson);

在此处输入图片说明

Check if the final json which is being passed is in the exact format as expected by the server.

Try giving:

contentType: 'application/json', Accept: 'application/json'

See if it helps.

try this one

 formData = {
        // all your parameters here
        param1: param1,
        JSONData: formToJSON(),
        UserId: providerKey
    }
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: url,
        dataType: "json",
        data: formData,
        success: function(data) {
            //success handling
        },
        error: function(data) {
            alert("Err : " + err.error);
        }
    });

function formToJSON() {
    return JSON.stringify({
        dataValue: dataValue
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM