通过JQuery Ajax发布数据

时间:2018-05-03 09:01:29

标签: jquery api

我试图通过JQuery Ajax调用api

以下是我的代码

$.ajax({
            url: 'http://www.ServerName.com/enquiry/report?recordStr=MFEF,1,BP03089999_USER,India@12,,6646,40,8,PMFI,Ruben,Damion,Pitts,Ruben,VTR0400108,,PANPN1010C,9201740108,ELECTRICITY BILL,EXBL0108,JOB CARD,JOBC010,OTHER ID,OTID0108,P03,8500176108,,,21121969,M,,,,,,C,Pitts Nivasthan,,MH,400108,Damion,K01,Damion,K01,Selene,K02,,,,,Damion,K01,,,,,ES',
            method: 'POST',
            contentType: "application/json",
            success: function(d) {
                $("#response").text("Success");
            },
            error: function(e) {
                $("#response").text("Error please try again");
            }
        });

api的基地址是

http://www.ServerName.com/enquiry/report

并且输入是长字符串

MFEF,1,BP03089999_USER,India@12,,6646,40,8,PMFI,Ruben,Damion,Pitts,Ruben,VTR0400108,,PANPN1010C,9201740108,ELECTRICITY BILL,EXBL0108,JOB CARD,JOBC010,OTHER ID,OTID0108,P03,8500176108,,,21121969,M,,,,,,C,Pitts Nivasthan,,MH,400108,Damion,K01,Damion,K01,Selene,K02,,,,,Damion,K01,,,,,ES

API将在名为recordStr

的参数中接收它

现在我面临两个问题:

  1. 如何在ajax调用中使用data属性发送参数?
  2. 我尝试了下面的代码,但它不起作用

    var recordStr = "MFEF,1,BP03089999_CIBILBANKUSER,India@123,,6646,40,8,CIBIL MFI,Ruben,Damion,Pitts,Ruben,VTR0400108,,PANPN1010C,9201740108,ELECTRICITY BILL,EXBL0108,JOB CARD,JOBC010,OTHER ID,OTID0108,P03,8500176108,,,21121969,M,,,,,,C,Pitts Nivasthan,,MH,400108,Damion,K01,Damion,K01,Selene,K02,,,,,Damion,K01,,,,,ES"
    
            $.ajax({
                url: 'http://www.cibilhawk.com/MFI/enquiry/comboreport/,
                method: 'POST',
                dataType: 'JSON',
                data: recordStr,
                contentType: "application/json",
                success: function(d) {
                    $("#response").text("Success");
                },
                error: function(e) {
                    $("#response").text("Error please try again");
                }
            });
    
    1. 此api适用于Postman并以XML格式返回数据,但在浏览器中出现错误Failed to load API , Response for preflight is invalid (redirect). Status Code: 307 Temporary Redirect
    2. 我如何解决这两个问题?

1 个答案:

答案 0 :(得分:0)

您需要将您的记录字符串包装到JSON对象中,如下所示。这应该可以解决问题。

// create a JSON object, instead of string
var recordStr = {'recordStr':"MFEF,1,BP03089999_CIBILBANKUSER,India@123,,6646,40,8,CIBIL MFI,Ruben,Damion,Pitts,Ruben,VTR0400108,,PANPN1010C,9201740108,ELECTRICITY BILL,EXBL0108,JOB CARD,JOBC010,OTHER ID,OTID0108,P03,8500176108,,,21121969,M,,,,,,C,Pitts Nivasthan,,MH,400108,Damion,K01,Damion,K01,Selene,K02,,,,,Damion,K01,,,,,ES"}

    $.ajax({
        url: 'http://www.cibilhawk.com/MFI/enquiry/comboreport/,
        method: 'POST',
        dataType: 'JSON',
        data: recordStr,
        contentType: "application/json",
        success: function(d) {
            $("#response").text("Success");
        },
        error: function(e) {
            $("#response").text("Error please try again");
        }
    });