ASP.NET: jQuery AJAX 'data' param problem

前端 未结 3 1290
温柔的废话
温柔的废话 2020-12-16 13:49

I\'ve been having problems with this code I had spent the last 3 hours digging around and trying to find an answer. As I wasn\'t successful, I will just post the code and as

相关标签:
3条回答
  • 2020-12-16 14:22

    If you're working with JSON-enabled .NET WebServices/WebMethods... my tips are:

    • Be very careful with web.config configuration. Use it to enable big parameters, POST method and JSON.

    • Use a framework to handle Object serialization e deserialization. I would recommend NewtonSoft's Json.NET.

    I don't think ASP.NET automagically do it for you, your parameters are always strings. You should take that strings, deserialize it and turn it to an array of objects.

    0 讨论(0)
  • 2020-12-16 14:24

    Try passing the data as a string, not an object, ie:

    $.ajax( {
        ...
        data : '{ a: 2, b: 3 }',
        ...
    } );
    

    The reasoning for this is that if you specify an object as data then jQuery serializes the data using query string format, whereas the server is expecting JSON format directly.

    This happens despite telling jQuery to use JSON as the data type - it seems to only relate to the result, not the request payload sent to the server.

    Everything else you have there looks correct to me.

    0 讨论(0)
  • 2020-12-16 14:42

    Although this is an older post I thought I would contribute. I have been sending an associative array same idea an the accepted post I just find it easier to write.

    Javascript

    postData[0] = 'data!';
    postData[1] = 'moar data!';
    postData[2] = 'and some data';
    
    $.ajax({
        type: 'POST',
        url: 'postUrl',
        data: { data: postData },
    });
    

    PHP

    $data = $_POST['data'][0];
    $moar_data = $_POST['data'][1];
    $and_some_data = $_POST['data'][2];
    
    0 讨论(0)
提交回复
热议问题