简体   繁体   English

如何将查询字符串参数传递给数据的Ajax调用

[英]How to pass query string parameter to ajax call from data

I'm using ajax call for hit the API & i want to send an query parameter, How can i send query parameter with ajax call. 我正在使用ajax调用来命中API,我想发送查询参数,我该如何使用ajax调用发送查询参数。 Here is my code. 这是我的代码。 Any help on this will be appreciated, Thanks. 谢谢您的帮助。

    $("[id*=btnok]").click(function (e) {
    e.preventDefault();
    var obj = {};
    obj = $.trim($("[id*=nodays]").val());
    $.ajax({
        url: "/apilink",
        "data": {
                "api": "api",
                "params": "?userType=driver&type=true&count=" + obj 
            },
        type: "post",
        dataType: "json",
        success: function (r) {
            console.log(r)
        }
    });
});

As you're sending a POST request any properties in the data object will be added to the body of the request. 在发送POST请求时, data对象中的所有属性都会添加到请求的正文中。 To force a value to be sent in the querystring you need to manually append it to the string you provide to the url property. 要强制在查询字符串中发送值,您需要将其手动添加到提供给url属性的字符串中。

Also note that defining obj as an empty object is redundant as $.trim() always returns a string. 还要注意,将obj定义为空对象是多余的,因为$.trim()始终返回字符串。

$("[id*=btnok]").click(function (e) {
  e.preventDefault();
  var obj = $.trim($("[id*=nodays]").val());

  $.ajax({
    url: "/apilink?params=" + obj,
    data: {
      api: "api",
    },
    type: "post",
    dataType: "json",
    success: function (r) {
      console.log(r)
    }
  });
});

As I see you make a post request so parameters are passed in params property in request body. 如我所见,您发出了发布请求,因此参数在请求正文的params属性中传递。 But as an option you can add string params to your /apilink . 但是,您可以选择将字符串参数添加到/apilink Like that: /apilink?param1=val1&param2=val2 像这样: /apilink?param1=val1&param2=val2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jQuery将查询字符串传递给Ajax调用? - How to pass a query string to Ajax call with jQuery? ajax数据参数如何传递包含查询字符串网址的json - ajax data parameter how to pass json including query-string urls 如何将数据传递给AJAX调用 - How to pass data to AJAX call 如何在Ajax调用中将参数传递给Controller方法? - How to pass a parameter to a Controller method on Ajax call? AJAX调用数据参数中的字符串插值 - String interpolation in AJAX call data parameter 如何使用 AJAX 中的查询字符串将数据/值传递给 controller - How to pass data/value to controller by using query string in AJAX 我如何使用javascript中的ajax调用将参数或ajax数据传递给python脚本并在成功时获得响应? - how do i pass parameter or ajax data to a python script using ajax call in javascript and get response if it is successful? 如何将数据从控制器传递给Ajax调用AngularJS的指令 - How to pass data from controller to directive from ajax call AngularJS Javascript - 如何将来自 ajax 调用的数据传递给 3 个函数? - Javascript - How to pass data from an ajax call to 3 functions? 如何将此数据传递到表单提交调用(类似于ajax帖子的数据参数)? - How can I pass in this data into a form submit call (similar to data parameter for an ajax post)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM